mysql - Join album with its images count and cover? -



mysql - Join album with its images count and cover? -

i'm trying results 2 tables using mysql, below format of 2 tables:

albums images ------------------------------ ---------------------------- id | name | cover id | image | album ------------------------------ ---------------------------- 1 | album1 | 3 1 | image1.jpg | 2 2 | album2 | 1 2 | image2.jpg | 3 3 | album3 | 0 3 | image3.jpg | 1 4 | album4 | 0 4 | image4.jpg | 1 ------------------------------ 5 | image5.jpg | 3 6 | image6.jpg | 1 ----------------------------

now given tables above, i'd result this:

--------------------------------------- id | name | count | cover --------------------------------------- 1 | album1 | 3 | image3.jpg 2 | album2 | 1 | image1.jpg 3 | album3 | 2 | null 4 | album4 | 0 | null --------------------------------------- count number of images in given album cover image id matches in albums cover column

i have tried few queries none of them give me desired results.

select a.id, a.name, count(p.id) imagecount, (select image albums, photos albums.cover = photos.id) photos p right outer bring together albums on a.id=p.album grouping a.id;

which results in listing same cover image each row.

i tried this:

select a.id, a.name, count(p.id) imagecount, covertable.image photos p right outer bring together albums on a.id=p.album right outer bring together (select albums.id, image albums, photos albums.cover = photos.id) covertable on covertable.id = a.id grouping a.id;

that results in single row 1 album has cover image.

select a.id, a.name, count(p1.id) imagecount, p2.image cover albums left bring together photos p1 on p1.album = a.id left bring together photos p2 on p2.id = a.cover grouping a.id

demo

mysql sql join

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -