1

这个表。问题是*找到使用最多成分的比萨*

我知道答案很特别,但我不知道如何使用查询找到它。我尝试了很多不同的方法,但都没有奏效。提前致谢。

   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

我只需要知道使用最多成分的披萨的名称。答案是从食谱组中选择比萨饼 count( )>=all (select count( ) from recipe group by Pizza);

输出是:

比萨

特别(1排)

4

4 回答 4

6

...最多的成分

以下查询列出了所有比萨饼及其成分总数。LIMIT 1如果要显示成分最多的比萨,请添加。

SELECT pizza, COUNT(*) totalIngredients
FROM tableName
GROUP BY pizzaList
ORDER BY totalIngredients DESC
-- LIMIT 1

SQLFiddle 演示

然而,这个查询出现了一个问题。如果两个比萨共享最多的成分,则仅显示排序顺序中的第一个。要在顶部显示所有比萨饼,请使用以下代码:

SELECT pizza, COUNT(*) totalIngredients
FROM pizzaList
GROUP BY pizza
HAVING COUNT(*) = 
(
  SELECT MAX(totalCount)
  FROM
  (
    SELECT  COUNT(*) totalCount
    FROM    pizzaList
    GROUP BY pizza
  ) x
)

SQLFiddle 演示

于 2012-09-27T01:47:17.570 回答
0

这将是:

SELECT pizza, COUNT(pizza) AS ingredientcount FROM pizzas GROUP BY pizza ORDER BY ingredientcount DESC LIMIT 1;
于 2012-09-27T01:48:03.207 回答
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请注意,如果给定的比萨饼可能会列出两次相同的成分,则您可能需要在计数中添加 a 。

于 2012-09-27T01:50:19.727 回答
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)列表而不是单个条目。

于 2012-09-27T01:57:21.217 回答