开发者

How to do a Mysql SELECT statement based on IF condition?

开发者 https://www.devze.com 2023-03-24 20:40 出处:网络
I want to combine 2 mysql calls into one with and if condition. This is what I currently have: IF mysql_query(\"SELECT File FROM DB.ShoePics WHERE Cover=\'1\' AND ShoeID=\'$ShoeID\' LIMIT 1\")

I want to combine 2 mysql calls into one with and if condition. This is what I currently have:

IF
mysql_query("SELECT File FROM DB.ShoePics WHERE Cover='1' AND ShoeID='$ShoeID' LIMIT 1")
Else
mysql_query("SELECT File FROM DB.ShoePics WHERE Cover='0' AND ShoeID='$ShoeID' LIMIT 1")

I guess it would look something similar to this but I know its incorrect:

mysql_query("SELECT File FROM DB.ShoePics WHERE IF(Cover='1')ELSE(Cover='0') AN开发者_高级运维D ShoeID='$ShoeID' LIMIT 1")

Can someone show me how? thanks!


make your query like

SELECT File FROM DB.ShoePics WHERE (Cover='1' OR Cover='0') AND ShoeID='$ShoeID' LIMIT 1


SELECT File 
FROM DB.ShoePics 
WHERE  
    CASE WHEN CONDITION=1 THEN Cover='1' ELSE Cover='0' END
    AND ShoeID='$ShoeID' LIMIT 1"


mysql_query("SELECT File FROM DB.ShoePics WHERE (Cover='1' AND ShoeID='$ShoeID' LIMIT 1") OR (Cover='0' AND ShoeID='$ShoeID' LIMIT 1")

0

精彩评论

暂无评论...
验证码 换一张
取 消