MySQL query for searching subset of two tables -


i have 2 tables:

create table if not exists `comments` (     `id` int(11) not null auto_increment,     `photograph_id` int(11) not null,     `created` datetime not null,     `author` varchar(255) not null,     `body` text not null,     `email` varchar(255) not null,     `liked` int(11) not null,     primary key (`id`),     key `photograph_id` (`photograph_id`) ) 

and this:

create table if not exists `photographs` (     `id` int(11) not null auto_increment,     `user_id` int(11) not null,     `filename` varchar(255) not null,     `type` varchar(100) not null,     `size` int(11) not null,     `caption` varchar(255) not null,     `liked` int(11) not null,     primary key (`id`),     key `user_id` (`user_id`) ) 

i having trouble merging these 2 1 query. in query have sorting call of number of comments every photo have. in comments table, there column photograph_id, links photo id in photographs table. help.

for photo's 1 or more comments do:

select p.id, count(*) commentcount photographs p inner join comments c on (p.id = c.photograph_id) group p.id order commentcount desc 

if want photo's 0 comments do:

select p.id, count(c.id) commentcount photographs p left join comments c on (p.id = c.photograph_id) group p.id order commentcount desc 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -