这张表。问题是 * 找到使用最多种成分的披萨 *
我知道答案很特别,但我不知道如何使用查询找到它。我尝试了许多不同的方法,但没有用。提前谢谢。
pizza | ingredient | amount
------------+------------+--------
margarita | cheese | 120
margarita | spice | 5
ham | ham | 150
ham | spice | 5
napolitana | anchovies | 100
napolitana | olives | 75
napolitana | spice | 10
hawaiian | ham | 100
hawaiian | pineapple | 100
hawaiian | spice | 5
cabanossi | cabanossi | 150
cabanossi | spice | 10
siciliano | onion | 50
siciliano | capsicum | 75
siciliano | olives | 50
siciliano | anchovies | 50
siciliano | spice | 15
americano | salami | 120
americano | pepperoni | 75
americano | spice | 10
mexicano | onion | 75
mexicano | capsicum | 75
mexicano | mushroom | 50
mexicano | chilli | 25
mexicano | spice | 20
seafood | seafood | 200
seafood | spice | 5
garlic | garlic | 25
garlic | spice | 10
vegetarian | onion | 50
vegetarian | capsicum | 50
vegetarian | mushroom | 50
vegetarian | peas | 50
vegetarian | tomato | 50
vegetarian | spice | 5
mushroom | mushroom | 100
mushroom | spice | 5
special | cheese | 25
special | tomato | 25
special | ham | 25
special | anchovies | 25
special | olives | 25
special | mushroom | 25
special | bacon | 25
special | egg | 25
special | pineapple | 25
special | cabanossi | 25
special | salami | 25
special | capsicum | 25
special | onion | 25
special | peas | 25
special | seafood | 25
special | spice | 10
stagiony | ham | 75
stagiony | mushroom | 50
stagiony | olives | 50
stagiony | anchovies | 25
stagiony | spice | 10
我只需要知道使用过大部分成分的披萨的名称。答案是通过披萨计数()> =全部(从披萨的食谱组中选择计数())从食谱组中选择披萨;
,输出为:
特殊 (1排)
答案 0 :(得分:6)
......成分数量最多
以下查询列出了所有披萨及其成分总数。如果您想要展示成分数量最多的披萨,请添加LIMIT 1
。
SELECT pizza, COUNT(*) totalIngredients
FROM tableName
GROUP BY pizzaList
ORDER BY totalIngredients DESC
-- LIMIT 1
然而,此查询出现问题。如果两个比萨饼共享最多的配料,则只显示排序顺序中的第一个。要在顶部显示所有比萨饼,请使用以下代码:
SELECT pizza, COUNT(*) totalIngredients
FROM pizzaList
GROUP BY pizza
HAVING COUNT(*) =
(
SELECT MAX(totalCount)
FROM
(
SELECT COUNT(*) totalCount
FROM pizzaList
GROUP BY pizza
) x
)
答案 1 :(得分:0)
这将是:
SELECT pizza, COUNT(pizza) AS ingredientcount FROM pizzas GROUP BY pizza ORDER BY ingredientcount DESC LIMIT 1;
答案 2 :(得分:0)
假设amount
是该成分的 ,您的意思是成分的最大数量(即数量):
SELECT pizza
FROM tableName
GROUP BY pizza
ORDER BY SUM(amount) DESC
LIMIT 1
如果您的意思是不同成分的最大数量(即独特成分的数量):
SELECT pizza
FROM tableName
GROUP BY pizza
ORDER BY COUNT(ingredient) DESC
LIMIT 1
注意,如果可以为给定的披萨列出两次相同的成分,则可能需要在计数中添加DISTINCT
。
答案 3 :(得分:0)
你的问题的答案取决于一些事情。您是否想要最大量的成分,或者最重要的不同成分。另外,您只需要第一个或按顺序列表吗?
首先,我们假设您需要浇头的总量。对此的查询如下:
SELECT TOP (1) Pizza, sum(amount) as TotalAmount
FROM PizzaTable
GROUP BY Pizza
ORDER BY sum(amount) DESC
要获得的不仅仅是最上面一行,请删除TOP (1)
。
现在,让我们假设你想要成分的数量,而不是总量。对此的查询将是:
SELECT TOP (1) Pizza, count(*) as NumberOfIngredients
FROM PizzaTable
GROUP BY Pizza
ORDER BY count(*) DESC
再次,删除TOP (1)
列表而不是单个条目。