How do I perform an IF...THEN
in an SQL SELECT
statement?
For example:
SELECT IF(Obsolete = \'N\' OR InStock = \'Y\' ? 1 :
The case statement is your friend in this situation, and takes one of two forms:
The simple case:
SELECT CASE WHEN THEN
WHEN THEN
ELSE
END AS
FROM
The extended case:
SELECT CASE WHEN THEN
WHEN THEN
ELSE
END AS
FROM
You can even put case statements in an order by clause for really fancy ordering.
- 热议问题