简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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