MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 21st, 2013, 02:55 PM
tannercampbell tannercampbell is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 8 tannercampbell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 14 sec
Reputation Power: 0
Exclamation Multiple OR using IN - Help

Can anyone help me understand why this isn't working?

I'm attempting to say:

"Please MySQL Database, for the love of god, return me the rows that match any of these userid's and are associated with these assigncodes id and fall between these dates."

This is what I'm writing:

SELECT * FROM TBL_SCHED_MASTER WHERE USERID IN (11597, 11598, 11599, 11607, 11608, 11609, 11610, 11618, 11620, 11629, 11630, 11631, 11642, 11650, 11651, 11652, 12944, 21760) AND ASSIGNCODEID IN (963, 873, 871, 1178, 869) AND ASSIGNDATE BETWEEN '2013-03-01' and '2013-03-31';

And it's telling me to go diaf.

What have I done wrong?


Best,
Tanner Campbell

Reply With Quote
  #2  
Old February 21st, 2013, 02:59 PM
tannercampbell tannercampbell is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 8 tannercampbell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 14 sec
Reputation Power: 0
PS: I'm very new to MySQL and still very much learning ... perhaps seeing what I had typed originally would help you understand what I'm trying to accomplish:

Code:
DELETE FROM TBL_SCHED_MASTER 
WHERE USERID = 11597
OR USERID = 11598
OR USERID = 11599
OR USERID = 11607
OR USERID = 11608
OR USERID = 11609
OR USERID = 11610
OR USERID = 11618
OR USERID = 11620
OR USERID = 11629
OR USERID = 11630
OR USERID = 11631
OR USERID = 11642
OR USERID = 11650
OR USERID = 11651
OR USERID = 11652
OR USERID = 12944
OR USERID = 21760
AND
ASSIGNCODEID = 963
OR ASSIGNCODEID = 873
OR ASSIGNCODEID = 871
OR ASSIGNCODEID = 1178
OR ASSIGNCODEID = 869
AND
ASSIGNDATE BETWEEN '2013-03-01' AND '2013-03-31';


Slightly embarrassing, really.

Reply With Quote
  #3  
Old February 21st, 2013, 03:26 PM
sr sr is offline
Problem Solver
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Jan 2001
Location: Stockholm, Sweden
Posts: 4,430 sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level)sr User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 7 h 23 m 50 sec
Reputation Power: 532
Why do you think your IN() query is wrong?

Based on the description you give:
-"... return me the rows that match any of these userid's and are associated with these assigncodes id and fall between these dates."
That query is correct.



You OR ... OR .... AND ... OR ... version of the query on the other hand is not correct and will show much more rows than you actually want.
When you work with a bit more complex query with OR's and AND's I recommend using parenthesis to group the expressions explicitly and not rely on the operator precedence.
Your query is essentially written like this:
Code:
DELETE FROM TBL_SCHED_MASTER 
WHERE USERID = 11597
OR USERID = 11598
OR USERID = 11599
OR USERID = 11607
OR USERID = 11608
OR USERID = 11609
OR USERID = 11610
OR USERID = 11618
OR USERID = 11620
OR USERID = 11629
OR USERID = 11630
OR USERID = 11631
OR USERID = 11642
OR USERID = 11650
OR USERID = 11651
OR USERID = 11652
OR USERID = 12944
OR ( USERID = 21760 AND ASSIGNCODEID = 963 )
OR ASSIGNCODEID = 873
OR ASSIGNCODEID = 871
OR ASSIGNCODEID = 1178
OR ( ASSIGNCODEID = 869 AND ASSIGNDATE BETWEEN '2013-03-01' AND '2013-03-31')
;

So all records are deleted for all the userid's specified except:
21760 where assigncodeid also must be 963

And all records for all the assigncodeid's except the 869 where it must also be between the dates specified.

Not quite what you had in mind right.

Ref: http://dev.mysql.com/doc/refman/5.6...precedence.html
__________________
/Stefan

Last edited by sr : February 21st, 2013 at 03:30 PM.

Reply With Quote
  #4  
Old February 21st, 2013, 04:23 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,371 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 32 m 9 sec
Reputation Power: 4140
Quote:
Originally Posted by tannercampbell
And it's telling me to go diaf.
dude, i never seen that error message before!!

congratulations!!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #5  
Old February 21st, 2013, 04:31 PM
tannercampbell tannercampbell is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 8 tannercampbell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 48 m 14 sec
Reputation Power: 0
Thumbs up

Quote:
Originally Posted by r937
dude, i never seen that error message before!!

congratulations!!


Custom NaviCat, only the worst MySQL guys have it.

Thank for your help SR. I thought it was wrong because it wasn't returning any results ... turns out there was another value I needed to include (scheduleID) to get exactly what I was looking for. Thank you!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Multiple OR using IN - Help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap