![](/img/trans.png)
[英]How to apply IF condition with WHERE CLAUSE in this Query Using MySql
[英]mysql query apply condition to where clause only if certain condition satisfied
我想通过测试condition_1将condition_2添加到mysql where子句中。 condition_1 是type = 'group:1'
并且 condition_2 是username = 'rahim'
。 因此,如果满足type = 'group:1'
,那么我将在 where 子句中添加username = 'rahim'
。 如果不满意,我不想将其添加到 where 子句中。 我试过username = 'rahim' CASE WHEN 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'
提前致谢。
你似乎想要:
WHERE NOT (type = 'group:1' AND username <> 'rahim')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.