sql case
程序员文章站
2022-06-05 12:40:27
...
WITH buy_fruit AS
(SELECT 'Sophia Liu' as name,
9 as price ,'apple' as fruit
UNION ALL SELECT 'Lisa Stelzner', 5,'pear'
UNION ALL SELECT 'Nikki Leith', 3 ,'banana'
UNION ALL SELECT 'Lauren Matthews', 11,'grape'
UNION ALL SELECT 'Lisa Stelzner', 20,'kiwi'
UNION ALL SELECT 'Nikki Leith', 12 ,'orange'
UNION ALL SELECT 'Lisa Stelzner', 2,'melon'
UNION ALL SELECT 'Lauren Matthews', 100,'cherry'
)
SELECT name, price, fruit ,
CASE
WHEN price < 20 THEN 'the fruit is cheap'
WHEN price<=50 THEN 'a little expansive'
ELSE 'too expansive'
END AS comment
FROM buy_fruit ;
name | price | fruit | comment |
Sophia Liu | 9 | apple | the fruit is cheap |
Lisa Stelzner | 5 | pear | the fruit is cheap |
Nikki Leith | 3 | banana | the fruit is cheap |
Lauren Matthews | 11 | grape | the fruit is cheap |
Lisa Stelzner | 20 | kiwi | a little expansive |
Nikki Leith | 12 | orange | the fruit is cheap |
Lisa Stelzner | 2 | melon | the fruit is cheap |
Lauren Matthews | 100 | cherry | too expansive |