Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL SELECT statement opposite to the one here.

Posted on 2004-04-28
7
Medium Priority
?
573 Views
Last Modified: 2008-01-09
Apologies for the poor question title.

I have three tables:

Tbl_AddressBook has these fields:
ContactID (Autonumber)
FirstName (text e.g. Alex)
LastName (text e.g. Harris)

Tbl_Groups has these fields:
GroupID (Autonumber)
GroupName (text e.g. Clients)

Tbl_GroupLinks has these fields:
LinkID (Autonumber)
ContactID (Number)
GroupID (Number)

Each member of the address book may or may not belong to a group.  They may belong to many groups, but only belong to the same group once.

I have created the following query to display the members of a particular group:

SELECT Tbl_Groups.GroupName, Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM (Tbl_GroupLinks INNER JOIN Tbl_AddressBook ON Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID) INNER JOIN Tbl_Groups ON Tbl_GroupLinks.GroupID=Tbl_Groups.GroupID
WHERE Tbl_GroupLinks.GroupID=1;

What I require is the opposite of this, a query which displays all the members of Tbl_AddressBook that are not in Tbl_GroupLinks.GroupID=1.

Please help me to solve this.  I am sure that for an expert this is relatively simple.

Regards

Alex Harris
0
Comment
Question by:videnda
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 2

Expert Comment

by:ljian
ID: 10939343
SELECT Tbl_Groups.GroupName, Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM (Tbl_GroupLinks INNER JOIN Tbl_AddressBook ON Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID) INNER JOIN Tbl_Groups ON Tbl_GroupLinks.GroupID=Tbl_Groups.GroupID
WHERE not Tbl_GroupLinks.GroupID=1;
0
 

Author Comment

by:videnda
ID: 10939628
Not exactly what I am looking for ljian.  Your statement only returns a contact if they are a member of a group.  Also if a contact appears in multiple groups, it still displays the contact with your statement.  My raw data is:

ContactID FirstName LastName GroupName
30 Adam Threadgill
31 Alasdair Macdonald
32 Becky Simmonds Group1
33 Ben Looijenga Group2
34 Ben Yelton Group3
34 Ben Yelton Group1
35 Calli Tofts Group1
36 Christy Nevin Group1
37 Chris Dingley Group4
38 Claire Ford Group2
39 Dahlia Pereria
41 David George MacIntosh
42 Emma Stinton
43 Helen Guy-Mas
44 Jackie Cordy
45 James Dundas
46 James Ford Group3
46 James Ford Group2
46 James Ford Group1
47 Janet Willing
48 Johnny Quarrie
49 John Vickers
50 Jon Bowyer Group3
50 Jon Bowyer Group1

If I run my first query WHERE Tbl_GroupLinks.GroupID=1, I am returned:
ContactID FirstName LastName GroupName
32 Becky Simmonds Group1
34 Ben Yelton Group1
35 Calli Tofts Group1
36 Christy Nevin Group1
46 James Ford Group1
50 Jon Bowyer Group1

The solution I require, will display the remaining contacts e.g.:
ContactID FirstName LastName GroupName
30 Adam Threadgill
31 Alasdair Macdonald
33 Ben Looijenga Group2
37 Chris Dingley Group4
38 Claire Ford Group2
39 Dahlia Pereria
41 David George MacIntosh
42 Emma Stinton
43 Helen Guy-Mas
44 Jackie Cordy
45 James Dundas
47 Janet Willing
48 Johnny Quarrie
49 John Vickers

Note the following do not appear in the second list even though they are in other groups:
34 Ben Yelton Group1
46 James Ford Group1
50 Jon Bowyer Group1

I hope this clarifies what I meant by opposite.

Alex
0
 
LVL 2

Expert Comment

by:ljian
ID: 10940336
In this case you should use left (right ) outer join instead of inner join in the from clause which retrieves only the group members ( strict equivalence Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID )
I think this works! Try with RIGHT if LEFT doesn't work
SELECT Tbl_GroupLinks.GroupID, Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM (Tbl_GroupLinks LEFT OUTER JOIN Tbl_AddressBook ON Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID)
WHERE not Tbl_Groups.GroupID=1;

I don't see the point for the groups table though.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:videnda
ID: 10940571
That is closer to what I am looking for and returns all the Contacts not in the first query.  But it also returns contacts from the first query who are in another group.

I think I need a subquery or something similar to the following.  The syntax is probably wrong because I am an SQL amateur.  Perhaps you can help:

Select Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM Tbl_AddressBook
WHERE Tbl_AddressBook.ContactID NOT IN(SELECT Tbl_Groups.GroupName, Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM (Tbl_GroupLinks INNER JOIN Tbl_AddressBook ON Tbl_GroupLinks.ContactID=Tbl_AddressBook.ContactID) INNER JOIN Tbl_Groups ON Tbl_GroupLinks.GroupID=Tbl_Groups.GroupID
WHERE Tbl_GroupLinks.GroupID=1);

Probably completely wrong but I am trying!!

Your help is appreciated.

Alex
0
 
LVL 2

Accepted Solution

by:
ljian earned 1000 total points
ID: 10940725
yes ... but if you compare contactid's and the result of the subquery should return a set of contactid's
Try this:
 
Select Tbl_AddressBook.FirstName, Tbl_AddressBook.LastName
FROM Tbl_AddressBook
WHERE Tbl_AddressBook.ContactID NOT IN(
           SELECT Tbl_GroupsLinks.ContactID
           FROM Tbl_GroupLinks
           WHERE Tbl_GroupLinks.GroupID=1);
0
 

Author Comment

by:videnda
ID: 10940780
It worked.  Thank you for your very fast replies.

Alex
0
 
LVL 2

Expert Comment

by:ljian
ID: 10940796
I'm glad I could help!

Lucian
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question