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
| --------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE IF NOT EXISTS `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned NOT NULL auto_increment primary key,
`name` varchar(255) NOT NULL,
`sessionId` varchar(255) NOT NULL,
`ident` integer unsigned NULL DEFAULT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `test` (`name`,`sessionId`,`ident`) values
('test1',12,default),('test2',12,default),('test3',12,350),
('test4',25,default),('test5',25,33),('test6',25,default),('test7',25,49),
('test8',33,default),('test9',33,26),('testa',33,default),('testb',33,26)
--------------
--------------
select * from `test`
--------------
+----+-------+-----------+-------+
| id | name | sessionId | ident |
+----+-------+-----------+-------+
| 1 | test1 | 12 | NULL |
| 2 | test2 | 12 | NULL |
| 3 | test3 | 12 | 350 |
| 4 | test4 | 25 | NULL |
| 5 | test5 | 25 | 33 |
| 6 | test6 | 25 | NULL |
| 7 | test7 | 25 | 49 |
| 8 | test8 | 33 | NULL |
| 9 | test9 | 33 | 26 |
| 10 | testa | 33 | NULL |
| 11 | testb | 33 | 26 |
+----+-------+-----------+-------+
--------------
update `test` as t1
inner join ( select `sessionId`,any_value(`ident`) as ident
from `test`
where `ident` is not null
group by `sessionId`
having count(distinct `ident`) < 2
) as t2
on t2.`sessionId` = t1.`sessionId`
set t1.`ident` = t2.`ident`
--------------
--------------
select * from `test`
--------------
+----+-------+-----------+-------+
| id | name | sessionId | ident |
+----+-------+-----------+-------+
| 1 | test1 | 12 | 350 |
| 2 | test2 | 12 | 350 |
| 3 | test3 | 12 | 350 |
| 4 | test4 | 25 | NULL |
| 5 | test5 | 25 | 33 |
| 6 | test6 | 25 | NULL |
| 7 | test7 | 25 | 49 |
| 8 | test8 | 33 | 26 |
| 9 | test9 | 33 | 26 |
| 10 | testa | 33 | 26 |
| 11 | testb | 33 | 26 |
+----+-------+-----------+-------+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager