Jump to content

[SOLVED] MySQL seemingly failing simple AND/OR query


webent

Recommended Posts

MySQL Version - 4.1.22-standard-log

 

Hi, for the life of me I can't figure out why my query is failing, is it me or is it a bug? Correct me if I'm mistaken, but doesn't an "AND" statement require that it must meet the criteria on both sides of the statement? Here's my code with some values already in place...

 

SELECT DISTINCT product_line FROM db.products WHERE product_vendor = 'TestingVendor' AND product_line = 'Automotive' OR product_line = 'Collectibles' OR product_line = 'VIDEO CONFERENCING' OR product_line = 'VIDEO EDITING - HARDWARE'

 

Now, Automotive and Collectibles are not product_lines that belong to product_vendor = 'Testing_Vendor', so therefore shouldn't show up, but they are showing up, can someone please tell me what's going on with that?

When mixing AND and OR, use (..)s to indicate the correct logic

 

Is it

(A AND B) OR C

or

A AND (B OR C)

 

Another thing you can do in your case is use "IN"

 

SELECT DISTINCT product_line 
FROM db.products 
WHERE product_vendor = 'TestingVendor' 
AND product_line  IN ( 'Automotive' , 'Collectibles' , 'VIDEO CONFERENCING'  'VIDEO EDITING - HARDWARE')

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.