我有以下表格:
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 表是未知的..