Question

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..

Was it helpful?

Solution 2

So i managed to get it work like this

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

OTHER TIPS

Your SQL syntax can be simplified. Here is an example of how I'm thinking it should be created. Note that this is not a tested query but I think it works:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top