PDA

View Full Version : sql - using AND OR



Zaid_W1red
February 20th, 2007, 09:47 AM
Hey guys,

So I have this SQL select statement:

SELECT *, SUM(downloads) AS totaldownloads FROM stats WHERE day LIKE '20' AND month LIKE '02' AND year LIKE '2007' AND name LIKE '%test%' AND type = 'text' GROUP BY name

And I want to basically modify the statement so that as well as matching my name column, a column called 'path' also matches.

If I do this:

SELECT *, SUM(downloads) AS totaldownloads FROM stats WHERE day LIKE '20' AND month LIKE '02' AND year LIKE '2007' AND name LIKE '%test%' OR path = '%path%' AND type = 'text' GROUP BY name

Is it separating the statement into two conditions? In the below the bold section is what I see as possibly being statement B, non bold statement A.

SELECT *, SUM(downloads) AS totaldownloads FROM stats WHERE day LIKE '20' AND month LIKE '02' AND year LIKE '2007' AND name LIKE '%test%' OR path = '%path%' AND type = 'text' GROUP BY name

If that's the case what's the correct syntax??

If this doesn't make sense...someone pipe up an d let me know!

borrob
February 20th, 2007, 10:09 AM
SELECT *, SUM(downloads) AS totaldownloads FROM stats WHERE day LIKE '20' AND month LIKE '02' AND year LIKE '2007' AND ( name LIKE '%test%' OR path = '%path%' ) AND type = 'text' GROUP BY name
if you want to have all those whre name matches or path matches you'll have to use ( ) because otherwise
day LIKE '20' AND month LIKE '02' AND year LIKE '2007' AND name LIKE '%test%'
is the first condition
and
path = '%path%' AND type = 'text'
the second for the or

goodluck

Zaid_W1red
February 20th, 2007, 10:10 AM
Awesome man ...thank you!

bwh2
February 20th, 2007, 11:07 AM
if you're not using wildcards, you should use = instead of LIKE:


...
WHERE colNameOne LIKE '%text'
AND colNameTwo = 'meh'