[英]mysql query apply condition to where clause only if certain condition satisfied
I want to add condition_2 to mysql where clause by test condition_1.我想通过测试condition_1将condition_2添加到mysql where子句中。 condition_1 is
type = 'group:1'
and condition_2 is username = 'rahim'
. condition_1 是
type = 'group:1'
并且 condition_2 是username = 'rahim'
。 So, if type = 'group:1'
satisfied, then I going to add username = 'rahim'
in where clause.因此,如果满足
type = 'group:1'
,那么我将在 where 子句中添加username = 'rahim'
。 If not satisfied, I do not want to add this to where clause.如果不满意,我不想将其添加到 where 子句中。 I tried
username = 'rahim' CASE WHEN type = 'group:1'
.我试过
username = 'rahim' CASE WHEN type = 'group:1'
。 But, it output error:但是,它输出错误:
[Err] 1064 : for the right syntax to use near 'CASE WHEN type = 'group:1' AND type = 'group:1''
[Err] 1064:在'CASE WHEN type = 'group:1' AND type = 'group:1''附近使用正确的语法
SELECT
a.user_id,
b.username,
a.message,
a.type
FROM
livechat_message a
LEFT JOIN livechat_user b ON a.user_id = b.user_id
WHERE
username = 'rahim' CASE
WHEN type = 'group:1'
AND type = 'group:1'
Thanks in advance.提前致谢。
你似乎想要:
WHERE NOT (type = 'group:1' AND username <> 'rahim')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.