MySQL多表选择SUM查询

时间:2015-01-26 15:21:56

标签: mysql database join

我在数据库中有3个表,我需要执行一个查询,从一个表中选择所有行,并将其与列两个表的总和相连,然后对该总和进行排序。这些表适用于游戏。您有一个团队表,一个用户唯一的用户表,并分配给一个团队,其中users.team_id = teams.id列,最后是一个项目表,每个人每天收集的项目。每天该人输入收集的物品数量,然后将其总和。 items表由users.adid = items.user_id。

连接

我需要的是一个精选查询,它会为我提供所有团队的列表,这些团队仅限于2015年之前的团队,然后列出每个团队可以拥有多个团队成员的项目总和。然后按照从最大到最小的项目数排序。

示例:

| Team ID | Team Name | Total Items |
-------------------------------------
| 1       | Bravo     | 5674        |
| 567     | Charlie   | 16          |

我尝试过使用MySQL SUM(),但我遇到的问题是连接,分组或嵌套查询,这需要获得每个团队的每个用户项目的总和。

表格结构:

CREATE TABLE `teams` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL DEFAULT '',
  `captain_id` varchar(11) NOT NULL DEFAULT '',
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `team_id` int(11) NOT NULL,
  `adid` varchar(12) NOT NULL DEFAULT '',
  `email` varchar(100) NOT NULL DEFAULT '',
  `first_name` varchar(100) DEFAULT '',
  `last_name` varchar(100) DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `ADID` (`adid`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `items` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` varchar(11) NOT NULL DEFAULT '0',
  `items` int(3) NOT NULL DEFAULT '0',
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2 个答案:

答案 0 :(得分:0)

select t.id, t.name, t.total
from teams t
inner join users u on t.id = u.team_id
inner join ( select user_id, sum(items) as total
             from items
             where date >= '2015-01-01'
               and date < '2016-01-01'
             group by user_id) t on t.user_id =  u.id
             order by t.total desc;

答案 1 :(得分:0)

这应该可以解决问题:

SELECT t.id, t.name, sum(i.items) as totalItems  /* you will get 3 columns as result: "id", "name" and "totalItems" */
FROM teams t /* start selecting from the table "teams"... */
    INNER JOIN users u ON u.team_id = t.id /* ... join the users table ... */
    INNER JOIN items i ON i.user_id = u.adid /* ... and last but not least the items table */
WHERE year(t.date) = 2015 /* filter the teams by year */
GROUP BY t.id /* group the result by team id - you want just one line per team */
ORDER BY totalItems DESC /* order by total items */

我看到你的项目表中也是一个日期 - 如果你想过滤一年中的那个,那就这样做:

SELECT t.id, t.name, sum(i.items) as totalItems  /* you will get 3 columns as result: "id", "name" and "totalItems" */
FROM teams t /* start selecting from the table "teams"... */
    INNER JOIN users u ON u.team_id = t.id /* ... join the users table ... */
    INNER JOIN items i ON i.user_id = u.adid AND year(i.date) = 2015 /* ... and last but not least the items table (and filter by year) */
WHERE year(t.date) = 2015 /* filter the teams by year */
GROUP BY t.id /* group the result by team id - you want just one line per team */
ORDER BY totalItems DESC /* order by total items */

用于将items-table与user-table连接,您当前正在使用varchar加入一个int - (items.user_id with users.adid) - 为什么不将items.user_id与users.id一起加入?如果那不可行,我建议将adid从varchar切换到int。