简体   繁体   English

在WHERE子句mysql中使用条件语句

[英]using conditional statement in WHERE clause mysql

If keep_base_amount is null then I want to use country_id=236 in where clause, else I want to use keep_base_amount 's value as in country_id. 如果keep_base_amount为null,那么我想在where子句中使用country_id=236 ,否则我想像在country_id中那样使用keep_base_amount的值。

I have query something like this: 我有这样的查询:

SELECT * FROM account_treasury_local
WHERE
CASE WHEN keep_base_amount IS NOT NULL THEN country_id = keep_base_amount
ELSE country_id = '236' END

There is a record in database. 数据库中有一条记录。 But, I am getting nothing in result. 但是,我什么都没有得到。 Is there anything missing/wrong in above query. 在上面的查询中是否有任何缺失/错误。

As simple as this 就这么简单

SELECT * FROM account_treasury_local
WHERE (keep_base_amount IS NOT NULL AND country_id = keep_base_amount) OR (keep_base_amount IS NULL AND country_id = '236')

Have you tried: 你有没有尝试过:

SELECT * FROM account_treasury_local
WHERE
  (keep_base_amount IS NOT NULL AND country_id = keep_base_amount)
  OR
  (keep_base_amount IS NULL AND country_id = '236')

I'm not sure I'm understanding your question correctly. 我不确定我是否正确理解了您的问题。

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

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