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
| --------------
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 `post`
--------------
--------------
CREATE TABLE `post`
( `id` integer unsigned not null auto_increment primary key,
`post_status` char(10) not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `post` (`id`,`post_status`) values
(76242, '----'),
(76243, '----')
--------------
--------------
select * from `post`
--------------
+-------+-------------+
| id | post_status |
+-------+-------------+
| 76242 | ---- |
| 76243 | ---- |
+-------+-------------+
--------------
DROP TABLE IF EXISTS `test`
--------------
--------------
CREATE TABLE `test`
( `id` integer unsigned not null auto_increment primary key,
`post_id` integer unsigned not null,
`date_from` datetime not null,
`date_to` datetime not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`post_id`,`id`,`date_from`,`date_to`) values
(76241, 1827, '2016-08-20 08:00:00', '2016-08-21 12:00:00'),
(76242, 1826, '2016-08-20 08:00:00', '2016-08-21 12:00:00'),
(76242, 1825, '2016-08-19 08:00:00', '2016-08-19 12:00:00'),
(76242, 1824, '2016-08-12 08:00:00', '2016-08-12 12:00:00'),
(76242, 1823, '2016-08-05 08:00:00', '2016-08-05 12:00:00'),
(76243, 1822, '2016-07-29 08:00:00', '2016-08-18 12:00:00'),
(76243, 1821, '2016-07-22 08:00:00', '2016-08-22 12:00:00')
--------------
--------------
select * from `test`
--------------
+------+---------+---------------------+---------------------+
| id | post_id | date_from | date_to |
+------+---------+---------------------+---------------------+
| 1821 | 76243 | 2016-07-22 08:00:00 | 2016-08-22 12:00:00 |
| 1822 | 76243 | 2016-07-29 08:00:00 | 2016-08-18 12:00:00 |
| 1823 | 76242 | 2016-08-05 08:00:00 | 2016-08-05 12:00:00 |
| 1824 | 76242 | 2016-08-12 08:00:00 | 2016-08-12 12:00:00 |
| 1825 | 76242 | 2016-08-19 08:00:00 | 2016-08-19 12:00:00 |
| 1826 | 76242 | 2016-08-20 08:00:00 | 2016-08-21 12:00:00 |
| 1827 | 76241 | 2016-08-20 08:00:00 | 2016-08-21 12:00:00 |
+------+---------+---------------------+---------------------+
--------------
select t1.post_id,
count(t1.post_id) as qte,
t2.nbre
from test as t1
inner join ( select post_id,
count(*) as nbre
from test
group by post_id
) as t2
on t2.post_id = t1.post_id
where t1.date_to < '2016-08-22'
group by t1.post_id
having qte = t2.nbre
order by t1.post_id, t2.post_id
--------------
+---------+-----+------+
| post_id | qte | nbre |
+---------+-----+------+
| 76241 | 1 | 1 |
| 76242 | 4 | 4 |
+---------+-----+------+
--------------
update `post` as ta
inner join (
select t1.post_id,
count(t1.post_id) as qte,
t2.nbre
from test as t1
inner join ( select post_id,
count(*) as nbre
from test
group by post_id
) as t2
on t2.post_id = t1.post_id
where t1.date_to < '2016-08-22'
group by t1.post_id
having qte = t2.nbre
order by t1.post_id, t2.post_id
) as tb
on tb.post_id = ta.id
set ta.post_status = 'draft'
--------------
--------------
select * from `post`
--------------
+-------+-------------+
| id | post_status |
+-------+-------------+
| 76242 | draft |
| 76243 | ---- |
+-------+-------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager