Return other row if condition not does not match in mysql

xqkwcwgp  于 2022-12-22  发布在  Mysql
关注(0)|答案(1)|浏览(185)

Select country, price, cost from table1 where user_id = 1 and is_enabled = 1 and country = 'IN' and sender_id = 'TEXT'
The above query will return the records if where conditions are met. Now, I want query to return records even if the conditions are not met but by removing the sender_id from the where condition i.e
Select country, price, cost from table1 where user_id = 1 and is_enabled = 1 and country = 'IN'
Please help!
I tried using CASE When but didn't achieved the result.

7gs2gvoe

7gs2gvoe1#

This should help if you need records anyways.

SELECT country, price, cost FROM table1
WHERE user_id = 1 AND is_enabled = 1 AND country = 'IN' OR NOT sender_id = 'TEXT'

In another use case, you can get the sender_id column if matched by using the below query but it will give you null for all unmatched records.

SELECT country, price, cost, CASE WHEN sender_id <> 'TEXT' THEN sender_id ELSE NULL END AS sender_id
FROM table1 WHERE user_id = 1 AND is_enabled = 1 AND country = 'IN'

相关问题