473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Selecting on multiple tables

Hi there,

I'm new to sql and thus I'm having problems with a specific query which I
hope you guys can help me with.

Basicly I have a few tables which I'm trying to do a query on:

Table groups contains information about specific groups e.g. "Windows" or
"Unix".
Table users contains information about specific users e.g. "a", "b" or "c".
Table users_groups contain information about group relationship (a user can
be in multiple groups) e.g. (a, Windows), (b, Unix), (a, Unix).
In this case user c is ungrouped.

Now I'd like to find the users which does belong to group Windows and those
who do not:

select distinct username from users_groups where groupname = "Windows" order
by username asc;

This works pretty well for finding users in the specific group. In this case
the result is a.

However I'd like to get the opposite result (b and c) but I'm stuck.

The problem is that I'd like a list of all users excluding those which are
in "Windows"

Here is a partial query:

select distinct users.username from users left join users_groups on
users.username = users_groups.username where users_groups.username is null
order by users.username asc;

This only gives me those users who are not grouped at all. This mean that
user b is not in those results.

Please advise.

Thanks in advance.

-- Henrik


Aug 13 '06 #1
2 1341
Henrik Goldman (he************@mail.tele.dk) writes:
Table groups contains information about specific groups e.g. "Windows"
or "Unix". Table users contains information about specific users e.g.
"a", "b" or "c". Table users_groups contain information about group
relationship (a user can be in multiple groups) e.g. (a, Windows), (b,
Unix), (a, Unix).
In this case user c is ungrouped.

Now I'd like to find the users which does belong to group Windows and
those who do not:
...
However I'd like to get the opposite result (b and c) but I'm stuck.
SELECT u.username
FROM users u
WHERE NOT EXISTS (SELECT *
FROM users_groups ug
WHERE u.userid = ug.userid
AND ug.group = 'Windows')

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 13 '06 #2
SELECT u.username
FROM users u
WHERE NOT EXISTS (SELECT *
FROM users_groups ug
WHERE u.userid = ug.userid
AND ug.group = 'Windows')

Thanks thats perfect. I didn't even know you could do that syntax. I guess I
learned something new today.

-- Henrik
Aug 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert | last post by:
Hi All, I'm trying to solve this for a few days now and I just can't figure it out... I have three tables set up, I'll simplify them for this question: Table 1: HOTELS Columns: HOTEL_ID,...
3
by: james.dixon | last post by:
Hi I was wondering if anyone else had had this problem before (can't find anything on the web about it). I have three select elements (list boxes - from here on I'll refer to them as 'the...
1
by: Andy S. | last post by:
Hi, I'm running DB2 V 7.1 and I wish to write a SELECT query that queries tables in multiple tables in different databases on the same instance. Is this possible and if so, what should the FROM...
0
by: allyn44 | last post by:
HI--I have 2 tables Cut: cut ID, HistNumb, Block, date: Cut Id is the primary key, the other 3 fileds are indexed to be unique Slides: Cutid SlideID, and various other fields: there can be...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
1
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been...
6
by: Mike Wilson | last post by:
Dear Group, I have a heirarchical set of database tables, say - "order" and "order_type" and want to display a series of orders in a grid control, and in place of the order_type foreign key...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
2
by: larry | last post by:
I am working on a DB for family data, and in this application the data spans variable amount of rows in multiple tables (one for the adults data, one for "family", one for the kids, another for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.