Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql select query condition

I have two database columns in a mysql database, A and B. In a select query, I want to implement this logic:

Select the rows where A is 'X'. If A is not set in a row, then check and select the row only if column B='Y'.

So one can say that column B is a fallback for column A.

How could I construct a SELECT query with 'X' and 'Y' as inputs for the WHERE clause?

like image 549
reggie Avatar asked Apr 08 '26 18:04

reggie


1 Answers

Use boolean logic:

SELECT *
FROM table
WHERE A = 'X' OR (A IS NULL AND B = 'Y')
like image 93
Yamikuronue Avatar answered Apr 11 '26 09:04

Yamikuronue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!