查询问题,插入并匹配

时间:2019-05-13 15:46:02

标签: mysql sql

ID      food        MONTH   Cost
523     VEG         Jan     50.00 
523     HOT DOG     Feb     40.00 
523     VEG         Feb     55.00 
523     COOKIE      Feb     10.00 
984     VEG         Mar     60.00 
427                 Jan     15.00 
644     HOT DOG     Feb     35.00 
644     FRIES       Feb      2.00 
644     HOT DOG     Mar     40.00 

我需要在显示蔬菜的食物(food2)之后插入一列

where veg=Vegatable, HOT DOG=Hot Dog, COOKIE=Cookie, 

采用正确的格式(大写的首字母),如果不是热狗,cookie和蔬菜面包这三个中的一个,则等于其他

ID  Food    food2           MONTH   REVENUE
523 VEG     Vegetable       Jan           50.00 
523 HOT DOG Hot Dog         Feb           40.00 
523 VEG     Vegetable       Feb           55.00 
523 COOKIE  Cookie          Feb           10.00 
984 VEG     Vegetable       Mar           60.00 
427         Other           Jan           15.00 
644 HOT DOG Hot Dog         Feb           35.00 
644 FRIES   Other           Feb            2.00 
644 HOT DOG Hot Dog         Mar           40.00

1 个答案:

答案 0 :(得分:1)

Select ID, 
       Food, 
       CASE WHEN Food = ‘VEG’ THEN ‘Vegetable’ 
            WHEN Food = ‘HOT DOG’ THEN ‘Hot Dog’ 
            WHEN Food = ‘COOKIE’ THEN ‘Cookie’ 
            ELSE ‘Other’ END AS Food2, 
       MONTH, 
       REVENUE
FROM MyTable