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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| --------------
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 `matable`
--------------
--------------
CREATE TABLE `matable`
( `id` integer unsigned NOT NULL auto_increment primary key,
`nom` varchar(255) NOT NULL,
`version` integer unsigned NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `matable` (`nom`,`version`) values
('AAA', 2), ('AAB', 2), ('AAB', 3), ('AAF', NULL), ('AAK', 3), ('AAK', 4), ('AAK', 5)
--------------
--------------
select * from matable
--------------
+----+-----+---------+
| id | nom | version |
+----+-----+---------+
| 1 | AAA | 2 |
| 2 | AAB | 2 |
| 3 | AAB | 3 |
| 4 | AAF | NULL |
| 5 | AAK | 3 |
| 6 | AAK | 4 |
| 7 | AAK | 5 |
+----+-----+---------+
--------------
select TB1.NOM || case when TB2.NB > 1 then '-v' || TB1.VERSION end
from MATABLE TB1
inner join
( select NOM
, count(*) as NB
from MATABLE
group by NOM
) TB2
on TB1.NOM = TB2.NOM
--------------
+--------------------------------------------------------------+
| TB1.NOM || case when TB2.NB > 1 then '-v' || TB1.VERSION end |
+--------------------------------------------------------------+
| NULL |
| 1 |
| 1 |
| NULL |
| 1 |
| 1 |
| 1 |
+--------------------------------------------------------------+
--------------
explain select TB1.NOM || case when TB2.NB > 1 then '-v' || TB1.VERSION end
from MATABLE TB1
inner join
( select NOM
, count(*) as NB
from MATABLE
group by NOM
) TB2
on TB1.NOM = TB2.NOM
--------------
+----+-------------+------------+------+---------------+-------------+---------+--------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+-------------+---------+--------------+------+---------------------------------+
| 1 | PRIMARY | TB1 | ALL | NULL | NULL | NULL | NULL | 7 | NULL |
| 1 | PRIMARY | <derived2> | ref | <auto_key0> | <auto_key0> | 257 | base.TB1.nom | 2 | NULL |
| 2 | DERIVED | MATABLE | ALL | NULL | NULL | NULL | NULL | 7 | Using temporary; Using filesort |
+----+-------------+------------+------+---------------+-------------+---------+--------------+------+---------------------------------+
--------------
select
CASE
WHEN TB2.NB > 1 THEN concat(TB1.NOM,'-v',TB1.VERSION)
ELSE TB1.NOM
END
from MATABLE TB1
inner join
( select NOM
, count(*) as NB
from MATABLE
group by NOM
) TB2
on TB1.NOM = TB2.NOM
--------------
+-------------------------------------------------------------------------------+
| CASE
WHEN TB2.NB > 1 THEN concat(TB1.NOM,'-v',TB1.VERSION)
ELSE TB1.NOM
END |
+-------------------------------------------------------------------------------+
| AAA |
| AAB-v2 |
| AAB-v3 |
| AAF |
| AAK-v3 |
| AAK-v4 |
| AAK-v5 |
+-------------------------------------------------------------------------------+
--------------
explain select
CASE
WHEN TB2.NB > 1 THEN concat(TB1.NOM,'-v',TB1.VERSION)
ELSE TB1.NOM
END
from MATABLE TB1
inner join
( select NOM
, count(*) as NB
from MATABLE
group by NOM
) TB2
on TB1.NOM = TB2.NOM
--------------
+----+-------------+------------+------+---------------+-------------+---------+--------------+------+---------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+------+---------------+-------------+---------+--------------+------+---------------------------------+
| 1 | PRIMARY | TB1 | ALL | NULL | NULL | NULL | NULL | 7 | NULL |
| 1 | PRIMARY | <derived2> | ref | <auto_key0> | <auto_key0> | 257 | base.TB1.nom | 2 | NULL |
| 2 | DERIVED | MATABLE | ALL | NULL | NULL | NULL | NULL | 7 | Using temporary; Using filesort |
+----+-------------+------------+------+---------------+-------------+---------+--------------+------+---------------------------------+
--------------
select t1.id, count(t2.nom) as nbre,
concat(t1.nom, case when count(t2.nom)>1 then coalesce(concat('-v', t1.version), '') else '' end) as result
from matable as t1
inner join matable as t2
on t2.nom = t1.nom
group by t1.id, t1.nom
--------------
+----+------+--------+
| id | nbre | result |
+----+------+--------+
| 1 | 1 | AAA |
| 2 | 2 | AAB-v2 |
| 3 | 2 | AAB-v3 |
| 4 | 1 | AAF |
| 5 | 3 | AAK-v3 |
| 6 | 3 | AAK-v4 |
| 7 | 3 | AAK-v5 |
+----+------+--------+
--------------
explain select t1.id, count(t2.nom) as nbre,
concat(t1.nom, case when count(t2.nom)>1 then coalesce(concat('-v', t1.version), '') else '' end) as result
from matable as t1
inner join matable as t2
on t2.nom = t1.nom
group by t1.id, t1.nom
--------------
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using temporary; Using filesort |
| 1 | SIMPLE | t2 | ALL | NULL | NULL | NULL | NULL | 7 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager