PHPNewbie55 Posted September 15, 2013 Share Posted September 15, 2013 I am having a problem getting a small snippet of code to work correctly....Here is my code::: $pol = mysql_query("select * from lma_links where ( ON_SALE = 'Y' and SUB = '159' or SUB = '111' or SUB = '71' ) order by rand() desc limit $addrandlistcount "); It works... but the results sometimes contain items where the ON_SALE does not equal 'Y'Any help figuring this out would be greatly appreciated.... Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted September 15, 2013 Solution Share Posted September 15, 2013 You need to use parenthesis to force the order of your conditions. Right now, that statement will be evaluated like this order: (ON_SALE = 'Y' and SUB = '159') or SUB = '111' or SUB = '71' So as long as either sub=111 or sub=71 is true, the entire condition is true. The ON_SALE=Y condition is attached only to the SUB=159 condition. I'm assuming you want that ON_SALE=Y to be required regardless, but sub can be any of the values. You need to use parenthesis around the OR conditions to ensure they are evaluated first, then only if they are true, the on_sale=y is evaluated. ON_SALE = 'Y' and (SUB = '159' or SUB = '111' or SUB = '71') Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted September 15, 2013 Author Share Posted September 15, 2013 Thank you.... I looked at it so long I couldn't see it... very much appreciated..!!! Quote Link to comment Share on other sites More sharing options...
Barand Posted September 15, 2013 Share Posted September 15, 2013 and SUB = '159' or SUB = '111' or SUB = '71' can be written more easily as and SUB IN ('159', '111', '71') which then sidesteps the (...) problem Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.