如何根据IF条件做一个Mysql SELECT语句?

时间:2011-08-03 19:15:52

标签: mysql if-statement conditional-statements

我想将2个mysql调用合并为一个with和if条件。这就是我目前所拥有的:

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")

我想它会看起来与此类似,但我知道它不正确:

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

有人能告诉我怎么样吗?谢谢!

3 个答案:

答案 0 :(得分:0)

使你的查询像

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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