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

时间:2015-02-24 21:26:34

标签: php mysql

如果keep_base_amount为空,那么我想在where子句中使用country_id=236,否则我想在country_id中使用keep_base_amount的值。

我有这样的问题:

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

数据库中有记录。但是,我得不到任何结果。上面的查询中是否有任何遗漏/错误。

2 个答案:

答案 0 :(得分:3)

这很简单

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')

答案 1 :(得分:1)

你试过了吗?

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')

我不确定我是否正确理解了你的问题。