If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Select problem - I am a newbie

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-17-09, 12:48
dc_duo dc_duo is offline
Registered User
 
Join Date: Mar 2005
Posts: 11
Select problem - I am a newbie

Hi guys wonder if you can help. I am writing a query to return search results. The trouble is some of the critera returns invalid results. I think I need to put some () around the statement, but dont know where can someone help?

SELECT * FROM tblmilkers A
INNER JOIN tbldates B ON A.ID = B.milkerID
WHERE B.date BETWEEN '2009-05-17 00:00:00' AND '2009-08-30 23:59:59'
AND A.nearestTown = 143 OR A.otherWorkableTowns LIKE '|143|'
AND B.status = 1 AND shedPreference = 'Rotary' AND skillSet = 'Sole Charge or Assistant' AND milkingExperience = '0-1 years' AND age = '18 and under' ORDER BY B.date, A.ratePerMilking ASC
Reply With Quote
  #2 (permalink)  
Old 05-17-09, 13:45
dc_duo dc_duo is offline
Registered User
 
Join Date: Mar 2005
Posts: 11
Sorted it

Used a sub select
Reply With Quote
  #3 (permalink)  
Old 05-17-09, 13:55
dc_duo dc_duo is offline
Registered User
 
Join Date: Mar 2005
Posts: 11
Nope still not working

I take it back, any suggestions would be appreciated, I am tearing my hair out with this one.
Reply With Quote
  #4 (permalink)  
Old 05-17-09, 15:26
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
this is your WHERE clause --
Code:
WHERE B.date BETWEEN '2009-05-17 00:00:00' 
                 AND '2009-08-30 23:59:59'    -- this is c1
  AND A.nearestTown = 143                     -- this is c2
   OR A.otherWorkableTowns LIKE '|143|'       -- this is c3    
  AND B.status = 1                            -- this is c4
  AND shedPreference = 'Rotary'               -- this is c5
  AND skillSet = 'Sole Charge or Assistant'   -- this is c6
  AND milkingExperience = '0-1 years'         -- this is c7 
  AND age = '18 and under'                    -- this is c8
so this is equivalent to --
Code:
WHERE c1 AND c2 OR c3 AND c4 AND c5 AND c6 AND c7 AND c8
okay, now, ANDs take precedence over ORs, so this is evaluated as follows --
Code:
WHERE ( c1 AND c2 ) 
   OR ( c3 AND c4 AND c5 AND c6 AND c7 AND c8)
however, i'd be willing to bet that what you want is this --
Code:
WHERE c1 AND ( c2 OR c3 ) AND c4 AND c5 AND c6 AND c7 AND c8
tip: when mixing ANDs and ORs, always code your own parentheses to get the logical evaluation you need

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On