SQL Multiple FROM SELECT Adding count -


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            comment 1   1         0 2    1            comment 2   1         0 3    2            comment 1   1         1 4    2            comment 2   1         0 5    2            comment 3   1         0 6    2            comment 4   1         0 

and actual query selects packages , content created user 3 :

select t1.date, t1.active, user_profile.id 'user_id', user_profile.name, user_profile.avatar, package_content.* (    select package.id 'package_id', package.user_id 'user_id', package.date 'date', package.active 'active'    package    package.user_id = 3    order 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 t1.package_id desc, package_content.order_id asc 

what in select want add count column counts comments have view_by_package_author = 0 each package

i tryed :

... (select t2.count, package.id 'package_id', package.user_id 'user_id', package.date 'date', package.active 'active' package left join    (select count(package_comment.view_by_package_author) 'count'    package_comment, t1    package_comment.view_by_package_author = 0    , package_comment.package_id = t1.package_id   )t2  on t2.package_id = t1.package_id  package.user_id = 3 order package.id desc limit 0,20 )t1 

but gives error since t1 table unknown..

your sql syntax can simplified. here example of how i'm thinking should created. note not tested query think 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.* package left join package_comment         on package.package_id=package_comment.package_id        , 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 package.user_id = 3 group package.package_id order package.package_id desc, package_content.order_id asc 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -