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 c开发者_JAVA百科an 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?
Use boolean logic:
SELECT *
FROM table
WHERE A = 'X' OR (A IS NULL AND B = 'Y')
I think this should work:
SELECT * FROM table
WHERE (A='X') OR ((A IS NULL) AND (B='Y'))
SELECT * FROM table WHERE A='X' OR (A IS NULL AND B='Y')
精彩评论