-4

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

User_guid | Post Message | Discussion board | Total |

1 | 300 | 25 | 325 |

2 | 15 | 185 | 200 |

3 | 100 | 203 | 303 |

但我不知道怎么做。

我有这个查询的 Post Message 结果:

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 表的行加入。总交互应该是帖子消息和讨论板值的总和。有人可以帮助我吗?谢谢。

4

3 回答 3

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使用组合查询获得哪个值。

于 2013-10-22T14:16:57.933 回答
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
于 2013-10-22T14:16:02.857 回答
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
于 2013-10-22T14:20:39.387 回答