我正在报道一家酒店,我需要总结一些关于他们预订的董事会的信息。
现在我的输出看起来像这样:
SELECT meal.id, meal.title,
SUM(CASE WHEN meal.id >= 600 THEN '1' ELSE '0' END) as NoofMeals
FROM meal
JOIN itinerary i ON i.id = meal.journeyid
WHERE i.arrival BETWEEN '2015-01-01' AND '2015-01-01'
GROUP BY meal.title
我的输出如下:
ID | title | NoofMeals
608 | Dinner | 15
621 | Breakfast | 478
648 | Lunch | 74
649 | Breakfast Box | 5
655 | Lunch-Box | 7
659 | American breakfast | 73
671 | Continental breakfast | 102
674 | All Inclusive | 312
689 | All Inclusive from 3pm | 7
693 | Picnic | 47
我想要的输出看起来像是这样,并且汇集了不同的价值,如所有的早餐(早餐,早餐盒,美式早餐和欧式早餐)或所有全包(全包和全包从下午3点)。不幸的是,ID不是连续的。
title | NoofMeals
Dinner | 15
Breakfast | 658
Lunch | 81
All Inclusive | 319
Picnic | 7
我不再需要ID了,我只需要能够重命名用餐的标题。
我希望你能帮助我,请放轻松我,我对MySQL很陌生。
提前谢谢!
答案 0 :(得分:0)
“快速而肮脏”的解决方案将使用字符串函数:
SELECT
case
when meal.title like '%Breakfast%' then 'Breakfast'
when meal.title like '%All inclusive%' then 'All Inclusive'
else meal.title
end as title,
...
...
GROUP BY
case
when meal.title like '%Breakfast%' then 'Breakfast'
when meal.title like '%All inclusive%' then 'All Inclusive'
else meal.title
end
虽然正确的解决方案是添加包含膳食类别的列:
id | title | category
---|------------------------|--------------
1 | Breakfast | Breakfast
2 | American breakfast | Breakfast
3 | Continental breakfast | Breakfast
4 | All Inclusive | All Inclusive
5 | All Inclusive from 3pm | All Inclusive
并按meal.category
代替meal.title
加入特定膳食和小组。