I have these following tables :
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
And my actual query that selects the packages and all their content created by user 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
What i would like to do now is in that select i want to add a count column that counts all the comments that have view_by_package_author = 0 for EACH package
I tryed to do something like :
... 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
But it gives an error since t1 table is unknown..