RNK
RNK

Reputation: 5792

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.

Upvotes: 0

Views: 65

Answers (2)

Blah Argh
Blah Argh

Reputation: 302

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.

Upvotes: 1

Pinx0
Pinx0

Reputation: 1258

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

Upvotes: 3

Related Questions