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

x
?
Solved

MySQL NOT IN Query

Posted on 2012-04-01
6
Medium Priority
?
402 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
[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
  • 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 7

Accepted Solution

by:
micropc1 earned 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this article, we’ll look at how to deploy ProxySQL.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 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