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
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
SET session collation_connection=latin1_general_ci
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `Client`
--------------
--------------
CREATE TABLE `Client`
( `idClient` integer unsigned NOT NULL AUTO_INCREMENT primary key,
`raisonSociale` varchar(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `Client` (`idClient`, `raisonSociale`) VALUES
(6, 'Bronon & Cie'),
(9, 'Artemus & Cie'),
(15, 'Anomymous & Cie')
--------------
--------------
select * from Client
--------------
+----------+-----------------+
| idClient | raisonSociale |
+----------+-----------------+
| 6 | Bronon & Cie |
| 9 | Artemus & Cie |
| 15 | Anomymous & Cie |
+----------+-----------------+
--------------
DROP TABLE IF EXISTS `DescModule`
--------------
--------------
CREATE TABLE `DescModule`
( `id` integer unsigned NOT NULL primary key,
`produit` char(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `DescModule` (`id`, `produit`) values
(0, 'valeur par défaut'),
(6, 'nom du produit 6'),
(9, 'nom du produit 9')
--------------
--------------
select * from DescModule
--------------
+----+-------------------+
| id | produit |
+----+-------------------+
| 0 | valeur par défaut |
| 6 | nom du produit 6 |
| 9 | nom du produit 9 |
+----+-------------------+
--------------
SELECT cli.raisonSociale,
coalesce(com.produit, def.produit) as produit
FROM Client AS cli
LEFT JOIN DescModule AS com
ON com.id = cli.idClient
INNER JOIN DescModule def
ON def.id = 0
--------------
+-----------------+-------------------+
| raisonSociale | produit |
+-----------------+-------------------+
| Bronon & Cie | nom du produit 6 |
| Artemus & Cie | nom du produit 9 |
| Anomymous & Cie | valeur par défaut |
+-----------------+-------------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager