MySQL - 如何获得重复的相邻字段的总和

时间:2013-03-31 20:28:40

标签: mysql duplicates sum

我想得到每个披萨的总积分。因此,例如,焦糖洋葱应该有12个点,而早餐应该有5个。

SQLFIDDLE link here.以下是MySQL查询:

SELECT pizza_table.name, results_table.first_pick_points as points
FROM results_table, pizza_table
WHERE results_table.first_pick = pizza_table.id
UNION
SELECT pizza_table.name, results_table.second_pick_points
FROM results_table, pizza_table
WHERE results_table.second_pick = pizza_table.id
UNION
SELECT pizza_table.name, results_table.third_pick_points
FROM results_table, pizza_table
WHERE results_table.third_pick = pizza_table.id
UNION
SELECT pizza_table.name, results_table.fourth_pick_points
FROM results_table, pizza_table
WHERE results_table.fourth_pick = pizza_table.id
UNION
SELECT pizza_table.name, results_table.fifth_pick_points
FROM results_table, pizza_table
WHERE results_table.fifth_pick = pizza_table.id

这些是目前的结果:

NAME                POINTS
Caramelized Onion   5
Buffalo Wing        5
Caramelized Onion   5
BM²                 4
asdf                4
Chaco               4
Breakfast           3
Chaco               3
Cheese Bread        3
The Karlyn          2
Caramelized Onion   2
Chicken Bacon Ranch 2
Buffalo Wing        1
Breakfast           1
Breakfast           1

这是我想要的回复:

NAME                POINTS
Caramelized Onion   12
Chaco               7
Buffalo Wing        6
Breakfast           5
BM²                 4
asdf                4
Cheese Bread        3
The Karlyn          2
Chicken Bacon Ranch 2

如何达到我想要的效果?

3 个答案:

答案 0 :(得分:1)

您需要使用聚合,但您似乎还需要更改查询以在每个查询之间使用UNION ALL

select name, sum(points) Total
from
(
  SELECT pizza_table.name, results_table.first_pick_points as points
  FROM results_table
  INNER JOIN pizza_table
    ON results_table.first_pick = pizza_table.id
  UNION ALL
  SELECT pizza_table.name, results_table.second_pick_points
  FROM results_table
  INNER JOIN pizza_table
    ON results_table.second_pick = pizza_table.id
  UNION ALL
  SELECT pizza_table.name, results_table.third_pick_points
  FROM results_table
  INNER JOIN pizza_table
    ON results_table.third_pick = pizza_table.id
  UNION ALL
  SELECT pizza_table.name, results_table.fourth_pick_points
  FROM results_table
  INNER JOIN pizza_table
    ON results_table.fourth_pick = pizza_table.id
  UNION ALL
  SELECT pizza_table.name, results_table.fifth_pick_points
  FROM results_table
  INNER JOIN pizza_table
    ON results_table.fifth_pick = pizza_table.id
) d
group by name
order by total desc

请参阅SQL Fiddle with Demo

注意,我还调整了您的查询以使用ANSI JOIN语法(INNER JOIN)。

答案 1 :(得分:0)

//一个简单的小组应该做你想做的事:

SELECT name, SUM(points) AS points
FROM   table_name
GROUP BY name;

答案 2 :(得分:0)

您最后应使用UNION ALLGroup by条款 - 还添加SUM的使用

工作小提琴:http://sqlfiddle.com/#!2/882e3/156

Select name, SUM(points) From(
SELECT pizza_table.name, results_table.first_pick_points as points
FROM results_table, pizza_table
WHERE results_table.first_pick = pizza_table.id
UNION ALL
SELECT pizza_table.name, results_table.second_pick_points
FROM results_table, pizza_table
WHERE results_table.second_pick = pizza_table.id
UNION ALL
SELECT pizza_table.name, results_table.third_pick_points
FROM results_table, pizza_table
WHERE results_table.third_pick = pizza_table.id
UNION All
SELECT pizza_table.name, results_table.fourth_pick_points
FROM results_table, pizza_table
WHERE results_table.fourth_pick = pizza_table.id
UNION All
SELECT pizza_table.name, results_table.fifth_pick_points
FROM results_table, pizza_table
WHERE results_table.fifth_pick = pizza_table.id) x
Group by name