sql - mysql query hierarchic -
i need find sql query
create table album ( id_album integer not null primary key auto_increment, nom varchar(255) not null ); create table sous_album ( id_sous_album integer not null primary key auto_increment, nom varchar(255) not null, id_album integer references album(id_album) ); create table photo ( id_photo integer not null primary key auto_increment, nom varchar(255) not null, id_sous_album integer references sous_album(id_sous_album) ); insert album(id_album, nom) values ('1', 'album1'), ('2', 'album2'), ('3', 'album3') ; insert sous_album(id_sous_album, nom, id_album) values ('1', 'sous album 1', '1'), ('2', 'sous album 2', '1'), ('3', 'sous album 3', '1'), ('4', 'sous album 4', '2'), ('5', 'sous album 5', '2'), ('6', 'sous album 6', '3') ; insert photo(id_photo, nom, id_sous_album) values ('1', 'photo1', '1'), ('2', 'photo2', '1'), ('3', 'photo3', '1'), ('4', 'photo4', '1'), ('5', 'photo5', '2'), ('6', 'photo6', '2'), ('7', 'photo7', '3'), ('8', 'photo8', '3'), ('9', 'photo9', '3'), ('10', 'photo10', '4'), ('11', 'photo11', '5'), ('12', 'photo12', '6'), ('13', 'photo13', '6'), ('14', 'photo14', '6'), ('15', 'photo15', '6') ; select distinct album.nom, count(photo.id_photo) album, photo, sous_album album.id_album=sous_album.id_album , sous_album.id_sous_album=photo.id_sous_album group album.nom order album.nom`
this query give me name of album , number of photos want sous album , numbers of photo... can u me ?
the result :
nom | count(photo.id_photo) album1 | 9 album2 | 2 album3 | 4
i need result :
nom | count(photo.id_photo) album1 | 9 sous album 1 | 4 sous album 2 | 2 sous album 3 | 3 album2 | 2 sous album 4 | 1 sous album 5 | 1 album3 | 4 sous album 6 | 4
select x.* , y.* ( select a.nom , count(*) total album join sous_album ap on ap.id_album = a.id_album join photo p on p.id_sous_album = ap.id_sous_album group a.nom ) x join ( select a.nom nom_album , ap.nom nom_sous_ablum , count(*) total album join sous_album ap on ap.id_album = a.id_album join photo p on p.id_sous_album = ap.id_sous_album group a.nom , ap.nom ) y on y.nom_album = x.nom;
note particular query return album if there @ least 1 sous_album (and photo) associated it.
Comments
Post a Comment