小编han*_*fix的帖子

MySQL查询WHERE包括CASE还是IF?

奇怪的问题.

我的查询看起来像

SELECT DISTINCT ID, `etcetc`, `if/elses over muliple joined tables` FROM
    table1 AS `t1`
    # some joins, eventually unrelated in that context
WHERE
# some standard where statements, they work/

CASE 
    WHEN `t1`.`field` = "foo" THEN (`t1`.`anOtherField` != 123 AND `t1`.`anOtherField` != 456 AND `t1`.`anOtherOtherField` != "some String")
    WHEN `t1`.`field` = "bar" THEN `t1`.`aSecondOtherField` != 12345
    END

#ORDER BY CASE etc. Standard Stuff
Run Code Online (Sandbox Code Playgroud)

显然MySQL返回错误的rowcount,我认为我的问题在于WHERE ... CASE语句的逻辑.也许用括号?也许我应该选择运营商OR而不是AND?我的第二个WHEN也应该包括括号,即使我只比较一个字段?我应该使用IF而不是CASE吗?

基本上我想要排除一些具有特定值的行,如果字段中的特定值foo或 …

mysql case where

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

case ×1

mysql ×1

where ×1