Bonjour,
je cherche une requête SELECT qui répondrait à la question du titre. En fait, c'est assez compliqué car ça implique 4 tables (d'où, je pense, 3 jointures dans la requête).
Tout d'abord, le USER émet un "ticket" sur un produit (pour signaler un bug)(les produits sont des logiciels) ; ce ticket est stocké en bdd et la structure de la table ticket est :
Ce que je nomme "user" est dans cette table la colonne customer_SESA (le SESA est l'identifiant du user) et "produit" est dans la colonne product_name.
Code sql : 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 CREATE TABLE `ticket` ( `id_ticket` varchar(15) COLLATE utf8_bin NOT NULL, `product_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `assigned_group` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `submitted_date` date DEFAULT NULL, `last_resolved_date` date DEFAULT NULL, `summary` varchar(200) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `priority` varchar(6) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `status` varchar(13) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `submitter_SESA` int DEFAULT NULL, `customer_SESA` int DEFAULT NULL, `type_incident` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `source` varchar(12) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `first_country` varchar(30) COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY (`id_ticket`), KEY `FK_customer_SESA` (`customer_SESA`), CONSTRAINT `FK_customer_SESA` FOREIGN KEY (`customer_SESA`) REFERENCES `customer` (`customer_SESA`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
On veut donc savoir si ce "user" a une licence. Celles-ci sont stockées dans la table license :
Comme on peut le voir, celle-ci a une clé étrangère sur la table application : CONSTRAINT `FK_APPLICATION` FOREIGN KEY (`application_key`) REFERENCES `application` (`application_key`) ON DELETE CASCADE ON UPDATE CASCADE dont la structure est :
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 CREATE TABLE `license` ( `sesa` int NOT NULL, `application_key` smallint NOT NULL, `currently_active` tinyint(1) DEFAULT NULL, `activate_date` date DEFAULT NULL, `deactivate_date` date DEFAULT NULL, `first_country` varchar(30) COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY (`sesa`,`application_key`) USING BTREE, KEY `FK_APPLICATION` (`application_key`), CONSTRAINT `FK_APPLICATION` FOREIGN KEY (`application_key`) REFERENCES `application` (`application_key`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Le nom de l'application est donc stocké dans cette table. Dernière difficulté : le nom product_name de la table ticket diffère du nom application_name de la table application. J'ai donc créé la table matching_ticket_license pour lister les correspondances :
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 CREATE TABLE `application` ( `application_key` smallint NOT NULL AUTO_INCREMENT, `application_name` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '', `platform` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, `platform_owner` bigint DEFAULT NULL, `publisher` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL, PRIMARY KEY (`application_key`), UNIQUE KEY `UK_appli_name` (`application_name`) /*!80000 INVISIBLE */ ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Y a plus qu'à trouver une requête impliquant ces 4 tables pour répondre à la question du titre. Voici ce que j'ai essayé :
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 CREATE TABLE `matching_ticket_license` ( `product_name` varchar(30) COLLATE utf8_bin DEFAULT NULL, `application_name` varchar(30) COLLATE utf8_bin DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;Mais le résultat est vide alors que ici la colonne product_name de la table ticket vaut "AUTOCAD" et que selon la table license, le user "3016" a bien une licence sur "AUTOCAD". Où est mon erreur, SVP ?
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part SELECT l.sesa FROM license l JOIN application a ON a.application_key=l.application_key JOIN matching_ticket_license m ON m.application_name=a.application_name JOIN ticket t ON t.customer_SESA=l.sesa where t.customer_SESA='3016'
Partager