I need help translating this into a MySQL statement. I need to do something like:
WHERE IF 'field1 > 0' THEN 'field2 < field1'
Right now, I have this MySQL statement, but doesn't work?:
SELECT * FROM `my_table` WHERE IF(field1>0, field2<field1, 1=1) AND field3='1';
Please advise a solution for this. Thank you!
You only want to check field2 < field1
if field1 > 0
, so code it that way:
SELECT * FROM `my_table`
WHERE ((field1 > 0 AND field2 < field1) OR field1 <= 0)
AND field3 = '1';
You should be using a case statement:
SELECT * FROM `my_table`
WHERE
field2< CASE WHEN (field1>0) THEN field1 ELSE field2+1 END
AND field3='1';
You can achieve this by using logical ANDs and ORs.
WHERE (field1 > 0 AND field2 < field1) OR (field1 <= 0 AND <else part of the statement>)
The <else part of the statement>
is the condition that you want to check if field1
is not > 0
, unless there is no such a clause, in which case remove the OR and everything after it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.