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
|
--------------
START TRANSACTION
--------------
--------------
set session collation_connection = "latin1_general_ci"
--------------
--------------
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`
( `clef` integer unsigned NOT NULL auto_increment primary key,
`id` integer unsigned NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
INSERT INTO `test` (`id`,`date`) VALUES
(127,'2014-05-01'),(321,'2019-02-24'),(114,'2021-04-01'),(127,'2012-02-01'),(127,'2013-04-02'),
(144,'2022-04-01'),(321,'2020-02-01'),(444,'2020-04-01'),(127,'2022-07-01')
--------------
--------------
select * from `test` order by clef
--------------
+------+-----+------------+
| clef | id | date |
+------+-----+------------+
| 1 | 127 | 2014-05-01 |
| 2 | 321 | 2019-02-24 |
| 3 | 114 | 2021-04-01 |
| 4 | 127 | 2012-02-01 |
| 5 | 127 | 2013-04-02 |
| 6 | 144 | 2022-04-01 |
| 7 | 321 | 2020-02-01 |
| 8 | 444 | 2020-04-01 |
| 9 | 127 | 2022-07-01 |
+------+-----+------------+
--------------
alter table `test` add column `num` integer unsigned not null after `date`
--------------
--------------
update `test` as t1
left outer join ( select t3.id,
t3.date,
count(*) as num
from `test` as t3
left outer join `test` as t4
on t4.id = t3.id
and t4.date <= t3.date
group by t3.id,t3.date
) as t2
on t2.id = t1.id
and t2.date = t1.date
set t1.num = t2.num
--------------
--------------
select * from `test` order by clef
--------------
+------+-----+------------+-----+
| clef | id | date | num |
+------+-----+------------+-----+
| 1 | 127 | 2014-05-01 | 3 |
| 2 | 321 | 2019-02-24 | 1 |
| 3 | 114 | 2021-04-01 | 1 |
| 4 | 127 | 2012-02-01 | 1 |
| 5 | 127 | 2013-04-02 | 2 |
| 6 | 144 | 2022-04-01 | 1 |
| 7 | 321 | 2020-02-01 | 2 |
| 8 | 444 | 2020-04-01 | 1 |
| 9 | 127 | 2022-07-01 | 4 |
+------+-----+------------+-----+
--------------
COMMIT
--------------
Appuyez sur une touche pour continuer... |
Partager