0

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.

1
  • what is the record in your DB? because this should work but I'd prefer to use AND|OR for this and not CASE clause Commented Feb 24, 2015 at 21:33

2 Answers 2

3

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')
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.