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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `produits`
--------------
--------------
CREATE TABLE `produits`
( `ref_produit` integer unsigned NOT NULL auto_increment primary key,
`code_categorie` smallint unsigned NOT NULL,
`no_fournisseur` smallint unsigned NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `produits` (`ref_produit`,`code_categorie`,`no_fournisseur`) values
(17, 1, 1),
(33, 3, 5),
(42, 4, 2),
(45, 2, 7),
(66, 5, 5),
(77, 2, 2)
--------------
--------------
select * from `produits`
--------------
+-------------+----------------+----------------+
| ref_produit | code_categorie | no_fournisseur |
+-------------+----------------+----------------+
| 17 | 1 | 1 |
| 33 | 3 | 5 |
| 42 | 4 | 2 |
| 45 | 2 | 7 |
| 66 | 5 | 5 |
| 77 | 2 | 2 |
+-------------+----------------+----------------+
--------------
DROP TABLE IF EXISTS `details_commandes`
--------------
--------------
CREATE TABLE `details_commandes`
( `id` integer unsigned NOT NULL auto_increment primary key,
`no_commande` integer unsigned NOT NULL,
`ref_produit` integer unsigned NOT NULL,
CONSTRAINT `FK_PRODUITS` FOREIGN KEY (`ref_produit`) REFERENCES `produits` (`ref_produit`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `details_commandes` (`no_commande`,`ref_produit`) values
(1025, 17),
(1025, 33),
(1025, 45),
(1025, 77),
(1112, 17),
(1112, 66),
(1256, 42),
(1256, 77)
--------------
--------------
select * from `details_commandes`
--------------
+----+-------------+-------------+
| id | no_commande | ref_produit |
+----+-------------+-------------+
| 1 | 1025 | 17 |
| 2 | 1025 | 33 |
| 3 | 1025 | 45 |
| 4 | 1025 | 77 |
| 5 | 1112 | 17 |
| 6 | 1112 | 66 |
| 7 | 1256 | 42 |
| 8 | 1256 | 77 |
+----+-------------+-------------+
--------------
drop view if exists `temp`
--------------
--------------
create view `temp` as
select t1.no_commande,
t2.code_categorie,
t2.no_fournisseur
from details_commandes as t1
inner join produits as t2
on t2.ref_produit = t1.ref_produit
order by t1.no_commande
--------------
--------------
select * from `temp`
--------------
+-------------+----------------+----------------+
| no_commande | code_categorie | no_fournisseur |
+-------------+----------------+----------------+
| 1025 | 2 | 2 |
| 1025 | 1 | 1 |
| 1025 | 3 | 5 |
| 1025 | 2 | 7 |
| 1112 | 5 | 5 |
| 1112 | 1 | 1 |
| 1256 | 2 | 2 |
| 1256 | 4 | 2 |
+-------------+----------------+----------------+
--------------
select no_commande
from `temp` as t1
where exists ( select 1
from `temp` as t2
where t2.no_commande = t1.no_commande
and t2.code_categorie = 1
and t2.no_fournisseur = 1
)
and exists ( select 1
from `temp` as t3
where t3.no_commande = t1.no_commande
and t3.code_categorie = 2
and t3.no_fournisseur = 2
)
group by no_commande
order by no_commande
--------------
+-------------+
| no_commande |
+-------------+
| 1025 |
+-------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |