SQL Multiple FROM SELECT添加计数

时间:2013-08-13 08:49:16

标签: sql count multiple-select

我有以下表格:

user_profile
id | name | avatar
1    John   john.jpg
2    Jack   jack.jpg
3    Yves   yves.jpg

package
id | name       | date                | author_id   | active
1    package 1    2013-08-13 08:00:00   3             1
2    package 2    2013-08-13 09:00:00   3             1
3    package 3    2013-08-13 10:00:00   3             1

package_content
id | package_id | name    | description
1    1            Book 1    Desc book 1
2    1            Book 2    Desc book 2   
3    1            Book 3    Desc book 3
4    2            Book 1    Desc book 1
5    2            Book 2    Desc book 2
6    3            Book 3    Desc book 3
7    3            Book 4    Desc book 4
8    3            Book 5    Desc book 5

package_comments
id | package_id | comment           | user_id | view_by_package_author
1    1            This is comment 1   1         0
2    1            This is comment 2   1         0
3    2            This is comment 1   1         1
4    2            This is comment 2   1         0
5    2            This is comment 3   1         0
6    2            This is comment 4   1         0

我的实际查询选择用户3创建的包及其所有内容:

SELECT t1.date, t1.active, user_profile.id 'user_id', user_profile.name, user_profile.avatar, package_content.*
FROM (
   SELECT package.id 'package_id', package.user_id 'user_id', package.date 'date', package.active 'active'
   FROM package
   WHERE package.user_id = 3
   ORDER BY package.id DESC
   LIMIT 0,20
)t1
LEFT JOIN package_content ON package_content.package_id = t1.package_id
LEFT JOIN user_profile ON user_profile.id = t1.user_id
ORDER BY t1.package_id DESC, package_content.order_id ASC

我现在想要做的就是选择我想要添加一个计数列,该列计算所有对于每个包<_ strong_ 的view_by_package_author = 0的注释

我尝试做类似的事情:

... FROM
(SELECT t2.count, package.id 'package_id', package.user_id 'user_id', package.date 'date', package.active 'active'
FROM package
LEFT JOIN 
  (SELECT count(package_comment.view_by_package_author) 'count' 
  FROM package_comment, t1 
  WHERE package_comment.view_by_package_author = 0 
  AND package_comment.package_id = t1.package_id
  )t2 
ON t2.package_id = t1.package_id 
WHERE package.user_id = 3
ORDER BY package.id DESC
LIMIT 0,20
)t1

但由于t1表未知,它会出错。

2 个答案:

答案 0 :(得分:1)

您的SQL语法可以简化。这是一个我认为应该如何创建的例子。请注意,这不是经过测试的查询,但我认为它有效:

SELECT COUNT(package_comment.package_comment_id), package.date, package.active, user_profile.id 'user_id', user_profile.name, user_profile.avatar, product_content.*
FROM package
LEFT JOIN package_comment 
       ON package.package_id=package_comment.package_id 
      AND package_comment.view_by_package_author = 0
LEFT JOIN package_content 
       ON package_content.package_id = package.package_id
LEFT JOIN user_profile 
       ON user_profile.id = package.user_id
WHERE package.user_id = 3
GROUP BY package.package_id
ORDER BY package.package_id DESC, package_content.order_id ASC

答案 1 :(得分:0)

所以我设法让它像这样工作

SELECT t1.count, t1.date, t1.active, user_profile.id 'user_id', user_profile.name, user_profile.avatar, package_content.*
FROM (
   SELECT count(package_comment.package.id)'count', 'package_id', package.user_id 'user_id', package.date 'date', package.active 'active'
   FROM package
   LEFT JOIN package_comment ON package_comment.package_id = package.id AND package_comment.view_by_package_author  = 0
   WHERE package.user_id = 3
   GROUP BY package.id
   ORDER BY package.id DESC
   LIMIT 0,20
)t1
LEFT JOIN package_content ON package_content.package_id = t1.package_id
LEFT JOIN user_profile ON user_profile.id = t1.user_id
ORDER BY t1.package_id DESC, package_content.order_id ASC