SQL MULTIPLE COUNT AND JOIN

时间:2013-10-22 14:09:50

标签: mysql sql join count

我正在尝试做这样的事情:

User_guid | Post Message | Discussion board | Total |

1 | 300 | 25 | 325 |

2 | 15 | 185 | 200 |

3 | 100 | 203 | 303 |

但我不知道如何。

我有此查询的帖子消息结果:

SELECT COUNT( * ) as 'Quantidade' , users_entity.name , users_entity.username
FROM river 
JOIN users_entity 
ON river.subject_guid = users_entity.guid 
AND river.action_type = 'create'
AND river.view = 'river/object/thewire/create'
GROUP BY river.subject_guid ORDER BY 'Quantidade' DESC

我在此查询中有讨论结果:

SELECT COUNT( * ) as 'Quantidade' , users_entity.name , users_entity.username
FROM river 
JOIN users_entity 
ON river.subject_guid = users_entity.guid 
AND river.action_type = 'reply'
GROUP BY river.subject_guid ORDER BY 'Quantidade' DESC

但我不知道如何将这些结果作为users_entity表的行加入。总交互应该是发布消息和讨论板值的总和。有人可以帮帮我吗?感谢。

3 个答案:

答案 0 :(得分:1)

没有表格结构和样本数据很难说(这就是为什么你得到了下来的选票),但你可能需要这样的东西:

SELECT
  users_entity.name,
  users_entity.username,
  COUNT(CASE WHEN river.action_type = 'create' AND river.view = 'river/object/thewire/create' THEN 1 END) AS Post_Message,
  COUNT(CASE WHEN river.action_type = 'reply' THEN 1 END) AS Discussion_Board
FROM river
JOIN users_entity ON users_entity.guid = river.subject_guid
GROUP BY
  users_entity.name,
  users_entity.username
ORDER BY ???

我不确定您想要ORDER BY使用合并查询的值。

答案 1 :(得分:0)

也许这适合你:

SELECT data1.username, data1.count1, data2.count2, data1.count1+data2.count2 from
(
SELECT COUNT( * ) as 'count1' , users_entity.name , users_entity.username
FROM river 
JOIN users_entity 
ON river.subject_guid = users_entity.guid 
AND river.action_type = 'create'
AND river.view = 'river/object/thewire/create'
GROUP BY river.subject_guid ORDER BY 'Quantidade' DESC
) data1,
(
SELECT COUNT( * ) as 'count2' , users_entity.name , users_entity.username
FROM river 
JOIN users_entity 
ON river.subject_guid = users_entity.guid 
AND river.action_type = 'reply'
GROUP BY river.subject_guid ORDER BY 'Quantidade' DESC) data2 where data1.username = data2.username

答案 2 :(得分:0)

您可以尝试加入这样的两个查询

SELECT river_create.Quantidade AS create_q, river_reply.Quantidade AS reply_q, river_create.full_name, river_create.u_name, river_create.user_id
    FROM
    (SELECT COUNT( * ) as 'Quantidade' , users_entity.name AS full_name, users_entity.username AS u_name, users_entity.guid AS user_id
         FROM river 
         JOIN users_entity 
         ON river.subject_guid = users_entity.guid 
         AND river.action_type = 'create'
         AND river.view = 'river/object/thewire/create'
         GROUP BY river.subject_guid ORDER BY 'Quantidade' DESC) AS river_create
    INNER JOIN
    (SELECT COUNT( * ) as 'Quantidade' , users_entity.name , users_entity.username, users_entity.guid as reply_user_id
         FROM river 
         JOIN users_entity 
         ON river.subject_guid = users_entity.guid 
         AND river.action_type = 'reply'
         GROUP BY river.subject_guid ORDER BY 'Quantidade' DESC) AS river_reply
ON river_create.user_id = river_reply.reply_user_id