MySQL:单个查询来计算2个表中的多个列

时间:2011-06-14 23:56:55

标签: mysql count

我意识到有几个类似于此问题的问题(here are three),但我已阅读其中许多问题并无法对齐答案在我的特殊情况下(也就是说,我是一个脑卒中n00b)。

我的终极目标

创建“最受欢迎”群组的列表,其中最受欢迎的群组位于顶部。受欢迎程度取决于成员数量。每个小组都有成员和报价;我想运行一个查询以获得两个计数。

表结构

group_members

+--------------------+------------------+------+-----+---------+----------------+
| Field              | Type             | Null | Key | Default | Extra          |
+--------------------+------------------+------+-----+---------+----------------+
| member_id          | int(10)          | NO   | PRI | None    |                |
| group_id           | int(10)          | NO   | PRI | None    |                |
+--------------------+------------------+------+-----+---------+----------------+

group_quotes

+--------------------+------------------+------+-----+---------+----------------+
| Field              | Type             | Null | Key | Default | Extra          |
+--------------------+------------------+------+-----+---------+----------------+
| quote_id           | int(10)          | NO   | PRI | None    |                |
| group_id           | int(10)          | NO   | PRI | None    |                |
+--------------------+------------------+------+-----+---------+----------------+

group_data

+--------------------+------------------+------+-----+---------+----------------+
| Field              | Type             | Null | Key | Default | Extra          |
+--------------------+------------------+------+-----+---------+----------------+
| entry_id           | int(10) unsigned | NO   | PRI | None    | auto_increment |
| title              | varchar(100)     | NO   |     | None    |                |
| url_title          | varchar(75)      | No   |     | None    |                |
+--------------------+------------------+------+-----+---------+----------------+

CURRENT(BROKEN)QUERY

我尝试过以下内容(以及它的近似变体),但结果是每个表中条目数的倍数:

SELECT group_quotes.group_id, COUNT(group_quotes.group_id) AS numquotes, COUNT(group_members.group_id) AS nummembers
FROM group_quotes, group_members
GROUP BY group_quotes.group_id
ORDER BY numquotes
DESC LIMIT 5

...within the query loop, output the group's title, etc...

例如:当我上次运行此查询时,group_members表中有7个条目,group_quotes表中有3个group_id#10个实例;对于组#10的成员计数和引用计数,我得到21(正确的数字应该是2和3)。

有人会介意看看,让我知道我哪里出错吗?

4 个答案:

答案 0 :(得分:1)

哎呀错误链接先前抱歉

如果我理解你的问题,

看起来像是一个类似的帖子

Sorting MYSQL Tag table

答案 1 :(得分:1)

<强>已更新

这样的事情应该有效:

SELECT    `group_data`.`id`,
          `group_data`.`title`,
          `group_data`.`url_title`,
          COUNT(DISTINCT `group_members`.`id`) AS  AS `group_members_count`, 
          COUNT(DISTINCT `group_quotes`.`id`) AS `group_quotes_count`
FROM      `group_data`
LEFT JOIN `group_members`
ON        `group_members`.`group_id`
LEFT JOIN `group_quotes`
ON        `group_quotes`.`group_id`
GROUP BY  `group_data`.`id`
ORDER BY  COUNT(`group_quotes`.`id`) DESC;

答案 2 :(得分:1)

我可能会错过这个问题,但我认为工会会做所需的事情

select group_id, count( member_id ), 'member' from group_members group by group_id
union all
select group_id, count( quote_id), 'quote' from group_quotes group by group_id 
order by 2

答案 3 :(得分:1)

这可能对你有用,还有:

SELECT gd.entry_id, c1.c, c2.c
FROM group_data gd
LEFT JOIN 
(SELECT gq.group_id group_id, COUNT(*) c
FROM group_quotes gq
GROUP BY gq.group_id) c1
ON c1.group_id = gd.entry_id
LEFT JOIN
(SELECT gm.group_id group_id, COUNT(*) c
FROM group_members gm
GROUP BY gm.group_id) c2
ON c2.group_id = gd.entry_id