postgresql - How to show albums that have at least one approved photo -


i have 2 tables: 1 album , other 1 album_photo. need show albums have @ least 1 approved photo. have following fields tables:

album id    name        date 1   myalbums      1-1-10 2   youralbum     2-3-10 3   somealbum     3-4-10   album_photo id album_id   photo_url   status 1    2        /some/url   pending   2    1        /some/url   approved 3    1        /some/url   approved 3    3        /some/url   declined 

in above table album id 1 ("myalbum") has approved photos, other 2 albums not have approved photos. want query displays names of albums have @ least 1 approved photo.

i tried this:

select a.*, a.id album_id, ap.*   album a,  album_photo ap a.id = ap.album_id , (select count(*) album_photo ap.status = 'approved') > 0; 

you query wrong in spite of joining them @ begaining join them inside clause.

select a.*,a.id album_id album (select distinct ap.album_id  album_photo ap  ap.album_id=a.id , ap.status='approved')>0; 

rest did correct. solution without distinct know ids unique distinct in inner query can removed , can written below.

select a.*,a.id album_id album  (select count(*)  album_photo ap  ap.album_id=a.id , ap.status='approved')>0; 

Comments