简体   繁体   English

MySQL在where子句中的情况

[英]MySQL case in where clause

I'm having problems with a suspected logic error with a CASE statement in my WHERE clause but I can't see the problem. 我的WHERE子句中的CASE语句存在可疑的逻辑错误,但我看不到问题。

SELECT a_id, a_name, a_folder_flag
FROM table_a
LEFT JOIN table_b ON table_a.a_id = table_b.b_a_id
WHERE a_status_id = 1
AND (
CASE
    WHEN a_access_flag = 1
    THEN b_usr_id = 1 OR b_grp_id = 2
    END )
GROUP BY a_id

Basically I want to get all records from table_a but if a_access_flag is 1 then I need to apply the additional where clause filtering in the CASE statement. 基本上,我想从table_a获取所有记录,但是如果a_access_flag1则需要在CASE语句中应用其他where子句过滤。

Currently it is returning 0 rows when I include the CASE statement. 目前,当我包含CASE语句时,它返回0行。

A case does not make much sense in a where clause. where子句中, case没有多大意义。 But you can convert that into logic. 但是您可以将其转换为逻辑。 Try 尝试

WHERE a_status_id = 1
AND 
(
  a_access_flag <> 1
  OR (b_usr_id = 1 OR b_grp_id = 2)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM