0

我有以下表格:

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

我现在想做的是在那个选择中我想添加一个计数列,该列计算每个包的 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 表是未知的..

4

2 回答 2

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
于 2013-08-13T08:59:22.987 回答
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
于 2013-08-13T09:39:07.380 回答