坚持与count查询的mysql连接

时间:2013-11-26 17:19:03

标签: mysql join count

我在加入2个查询时遇到问题但在第二个查询中我只想引入计数。

第一个查询效果很好

SELECT DISTINCT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid, members.first_name, views 
FROM forum_sub, members 
WHERE members.userid = forum_sub.userid AND forum_sub.forum_id = 1 
ORDER BY forum_sub.timestamp DESC

显示以下内容

----------------------------------------------------------------------
sub_id | forum_id | title | text | url | userid | first_name | views |
----------------------------------------------------------------------
  20   |    1     | test  | test |test | 1001   | JOhn       | 123   |
----------------------------------------------------------------------
   1   |    1     | test  | test |test | 1002   | Pete       |  23   |
----------------------------------------------------------------------
  10   |    1     | test  | test |test | 1003   | Harry      |  34   |                                                             
----------------------------------------------------------------------

但是现在我想将上面的sub_id加入到另一个名为forum_topics的表中,并计算有多少相同的sub_id,并带来该值

例如我可以使用

SELECT sub_id,COUNT(sub_id)as topics FROM forum_topics GROUP BY sub_id

-----------------
|sub_id | topics|
  ---------------
|   1   |   4   |
-----------------
|   10  |   3   |
-----------------
|   20  |   5   |
----------------- 

我的问题是如何加入这两个查询,以便我得到类似的内容

----------------------------------------------------------------------------
sub_id | forum_id | title | text | url | userid | first_name | views | count|
-----------------------------------------------------------------------------
  20   |    1     | test  | test |test | 1001   | JOhn       | 123   |  5   |
-----------------------------------------------------------------------------
   1   |    1     | test  | test |test | 1002   | Pete       |  23   |  4   |
-----------------------------------------------------------------------------
  10   |    1     | test  | test |test | 1003   | Harry      |  34   |  3  |                                                      
-----------------------------------------------------------------------------

任何帮助都会很棒,我知道我需要使用子查询,但我几乎整天都被困在这里,没有运气

2 个答案:

答案 0 :(得分:0)

尝试

group by sub_id

在第二个查询结束时而不是WHERE。 然后将它加入另一个表。

答案 1 :(得分:0)

首先尽可能避免使用SELECT DISTINCT。这是邪恶的。它将隐藏您在查询中的交叉连接等。从这个查询中很难说出你到底在做什么。

然而,为了包括计数,你有几个选择:

一种方法是在查询中进行子选择:

SELECT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid, 
members.first_name, views , (SELECT COUNT(sub_id) as topics FROM forum_topics WHERE sub_id = forum_sub.sub_id) count
FROM forum_sub, members 
WHERE members.userid = forum_sub.userid AND forum_sub.forum_id = 1 
ORDER BY forum_sub.timestamp DESC

另一种方法是实际加入你创建的子查询:

SELECT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid, 
       members.first_name, views, counts.topics
FROM forum_sub, members 
JOIN (SELECT sub_id, COUNT(sub_id) as topics FROM forum_topics GROUP BY sub_id) counts     ON (counts.sub_id = forum_sub.sub_id)
WHERE members.userid = forum_sub.userid AND forum_sub.forum_id = 1 
ORDER BY forum_sub.timestamp DESC

您还应该查看联接,而不是在from

中选择多个表
SELECT forum_sub.sub_id, forum_id, title, text, url, forum_sub.userid, 
       members.first_name, views, counts.topics
FROM forum_sub
JOIN members ON (members.userid = forum_sub.userid)
JOIN (SELECT sub_id, COUNT(sub_id) as topics FROM forum_topics GROUP BY sub_id) counts ON (counts.sub_id = forum_sub.sub_id)
WHERE forum_sub.forum_id = 1 
ORDER BY forum_sub.timestamp DESC