• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

MySQL Query Help


Ok.. I have query that goes something like:

SELECT * FROM table WHERE uniquefeild1 = '1' AND sharedfield1 = '1' OR uniquefield2 = '1'

so that works fine and does the query as it should, but when i change it to:

SELECT * FROM table WHERE uniquefeild1 = '1' AND sharedfield1 = '1' OR sharedfield2 = '1' OR uniquefield2 = '1'

it disregards the "uniquefield1" part and just shows anything the "sharedfield2" appears in. I'm guessing it is my use of AND/OR's...

Anyone know what the problem is?
Yes, it may be due to the usage of your AND operators... as when you use AND, both the left AND right objects must be true, otherwise unequefield1 will not work. The use of OR means that either unequefield1 OR the object on the right can be true, otherwise it will go onto the next part of the query.


SELECT * FROM <such_table> WHERE unequefield1='1' AND sharedfield1='1'

this will produce the SELECT * FROM <such_table> ONLY IF both unequefield1 AND sharedfield1 equal 1.

Hopefully this will help.
Thanks, but what I'm actually wanting it to select is a result that contains the "uniquefield1" required content and matches any of the sharedfield1, sharedfield2, uniquefield2 fields, not neccessarily all the fields, just one or more.