Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

issue with mysql query 'where' priority

Im having issues with this mysql query, i need it to bring ONLY the files that have the condition WHERE st.class_id in ('%s') as principal, and the other conditions as secundary, tried changing the or’s to and’s with no luck,
thanks in advance.

$wpdb->get_results($wpdb->prepare("SELECT `st`.id,`st`.sub_name,`st`.stime,`st`.edate FROM {$_tables['subject']} AS st WHERE `st`.class_id in ('%s') and (`stime` BETWEEN '%s' AND '%s') OR (`edate` BETWEEN '%s' AND '%s');",[$start,$end,$start,$end, $value]));

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Review operator precedence of AND versus OR.

You have a boolean expression in your WHERE clause like this:

WHERE A AND B OR C

Since AND has higher precedence than OR, it is evaluated as if you had done this:

WHERE (A AND B) OR C

That’s probably the source of your problem.

To override this, you need to use parentheses explicitly:

WHERE A AND (B OR C)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading