Solved

MySQL NOT IN Query

Posted on 2012-04-01
6
395 Views
Last Modified: 2012-06-27
I'm trying to get a list of names from one query that are not in the list of results from the second query:

Query 1 - Returns 289 users

SELECT tbl_Data.FirstName, tbl_Data.LastName, tbl_Data.ID
FROM tbl_Data
WHERE tbl_Data.CECMember =1

Open in new window


Query 2 - Returns 221 users

SELECT tbl_Data.FirstName, tbl_Data.LastName, tbl_Data.ID
FROM tbl_Data, tbl_Attendance
WHERE tbl_Data.ID = tbl_Attendance.VisitorID
AND tbl_Attendance.Date
BETWEEN  '2012-03-01'
AND  '2012-04-01'
AND (
tbl_Data.CECMember =  '1'
)
GROUP BY tbl_Attendance.VisitorID
ORDER BY  `tbl_Data`.`LastName` ASC 

Open in new window


Now, I'd like to get a list of the 68 users in Query 1 that are NOT in Query 2.

I tried using the following:

SELECT tbl_Data.FirstName, tbl_Data.LastName, tbl_Data.ID
FROM tbl_Data
WHERE tbl_Data.CECMember =1 and
tbl_Data.ID NOT IN
(
SELECT tbl_Data.FirstName, tbl_Data.LastName, tbl_Data.ID
FROM tbl_Data, tbl_Attendance
WHERE tbl_Data.ID = tbl_Attendance.VisitorID
AND tbl_Attendance.Date
BETWEEN  '2012-03-01'
AND  '2012-04-01'
AND
tbl_Data.CECMember =  '1'
GROUP BY tbl_Attendance.VisitorID
ORDER BY  `tbl_Data`.`LastName` ASC 
)

Open in new window


This gives me the following error message:  #1241 - Operand should contain 1 column(s)

Any help is greatly appreciated.
0
Comment
Question by:Jason92s
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:micropc1
ID: 37793637
Try EXCEPT...

SELECT tbl_Data.FirstName, tbl_Data.LastName, tbl_Data.ID
FROM tbl_Data
WHERE tbl_Data.CECMember =1

EXCEPT

SELECT tbl_Data.FirstName, tbl_Data.LastName, tbl_Data.ID
FROM tbl_Data, tbl_Attendance
WHERE tbl_Data.ID = tbl_Attendance.VisitorID
AND tbl_Data.CECMember =  '1'
AND tbl_Attendance.Date BETWEEN  '2012-03-01' AND  '2012-04-01'

Open in new window

0
 

Author Comment

by:Jason92s
ID: 37793645
I don't believe MySQL has a "MINUS" function.
0
 
LVL 10

Expert Comment

by:Tony Barkdull
ID: 37793648
You should use the name of the query like

SELECT query1.FirstName, query1.LastName, query1.ID
FROM query1

LEFT JOIN query2 ON query2.ID

WHERE query.CECMember =1 AND query1.ID IS NOT NULL

Something similar to that
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 7

Accepted Solution

by:
micropc1 earned 500 total points
ID: 37793654
You're right it doesn't...MINUS is Oracle and for some reason I was thinking your post said MSSQL, which would use EXCEPT... so boy, I was way off...

yeah, with MySQL you'll need to use IN. You're sub-select can only return a single column..

Try...



SELECT tbl_Data.FirstName, tbl_Data.LastName, tbl_Data.ID
FROM tbl_Data
WHERE tbl_Data.CECMember =1 and
tbl_Data.ID NOT IN
(
SELECT tbl_Data.ID
FROM tbl_Data, tbl_Attendance
WHERE tbl_Data.ID = tbl_Attendance.VisitorID
AND tbl_Attendance.Date
BETWEEN  '2012-03-01'
AND  '2012-04-01'
AND
tbl_Data.CECMember =  '1'
GROUP BY tbl_Attendance.VisitorID
ORDER BY  `tbl_Data`.`LastName` ASC 
)

Open in new window

0
 

Author Comment

by:Jason92s
ID: 37793671
That's it!  Man, I complete overlooked that.  Thank you so much!
0
 

Author Closing Comment

by:Jason92s
ID: 37793672
Very helpful, thank you!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Php  POSTGRES  "  IN SIDE "  " 4 78
How can i make performance tuning to my sql query? 6 48
paypal ipn to mysql 3 40
sort in mysql based off of query param 4 24
Fore-Foreword Today (2016) Maxmind has a new approach to the distribution of its data sets.  This article may be obsolete.  Instead of using the examples here, have a look at the MaxMind API (https://www.maxmind.com/en/geolite2-developer-package). …
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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