开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜