Bonjour,
J'ai 2 tables :
- table albums (id, titre)
- table titres (id, titre, album_id)
Je souhaite avoir le nombre de titres par album ?
Comment le faire?
Merci d'avance...
Bonjour,
J'ai 2 tables :
- table albums (id, titre)
- table titres (id, titre, album_id)
Je souhaite avoir le nombre de titres par album ?
Comment le faire?
Merci d'avance...
Bonjour,
je l'ai pas testée mais ça devrait s'en approcher
http://sqlpro.developpez.com/cours/sqlaz/ensembles/#L1
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 SELECT COUNT(titres.id), albums.titre FROM albums INNER JOIN titres ON titres.album_id = albums.id GROUP BY albums.titre
Bonjour,
personnellement, vu qu'un album contient n titres et qu'un titre peut se trouver dans n albums, j'aurais fait une table de jonction avec en clé primaire l'association des clés étrangères album_id et titre_id et l'attribut titre
intérêt : garantir l'intégrité
Quelque chose comme ca :
les tables :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; CREATE TABLE IF NOT EXISTS `albums` ( `id` int(11) NOT NULL AUTO_INCREMENT, `titre` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ; INSERT INTO `albums` (`id`, `titre`) VALUES (1, 'titre_album_1'), (2, 'titre_album_2'), (3, 'titre_album_3'); CREATE TABLE IF NOT EXISTS `album_titres` ( `album_id_fk` int(11) NOT NULL, `titre_id_fk` int(11) NOT NULL, PRIMARY KEY (`album_id_fk`,`titre_id_fk`), KEY `fk_titre` (`titre_id_fk`), KEY `fk_album` (`album_id_fk`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `album_titres` (`album_id_fk`, `titre_id_fk`) VALUES (1, 1), (2, 1), (1, 2), (1, 3), (2, 4), (1, 5), (3, 5), (3, 6), (1, 7), (3, 7), (3, 8), (1, 9), (2, 9), (3, 9), (2, 10), (3, 10); CREATE TABLE IF NOT EXISTS `titres` ( `id` int(11) NOT NULL AUTO_INCREMENT, `titre` varchar(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ; INSERT INTO `titres` (`id`, `titre`) VALUES (1, 'titre1'), (2, 'titre2'), (3, 'titre3'), (4, 'titre4'), (5, 'titre5'), (6, 'titre6'), (7, 'titre7'), (8, 'titre8'), (9, 'titre9'), (10, 'titre10'); ALTER TABLE `album_titres` ADD CONSTRAINT `album_titres_ibfk_3` FOREIGN KEY (`album_id_fk`) REFERENCES `albums` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, ADD CONSTRAINT `album_titres_ibfk_4` FOREIGN KEY (`titre_id_fk`) REFERENCES `titres` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
La requête
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 SELECT albums.titre, COUNT(*) as nb_titres FROM albums INNER JOIN album_titres ON album_titres.album_id_fk = albums.id INNER JOIN titres ON album_titres.titre_id_fk = titres.id GROUP BY albums.titre
Et voilà ton intégrité est respectée et cerise sur le gateau, le moteur innoDb te met à jour la table album_titres si tu supprime/update les tables albums et titrestitre_album_1 6
titre_album_2 4
titre_album_3 6
PS: crée la table album_titres en dernier dans ta base
Merci, je teste et reviens à vous les maîtres...![]()
Partager