Solved

SQL SELECT statement opposite to the one here.

Posted on 2004-04-28
7
563 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 250 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now