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
| --------------
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 `test`
--------------
--------------
CREATE TABLE `test`
( `product_id` integer unsigned not null auto_increment,
`shooper_group_id` integer unsigned not null,
`product_price` integer unsigned not null,
primary key (`product_id`,`shooper_group_id`)
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `test` (`product_id`,`shooper_group_id`,`product_price`) values
(1,5,10),(1,7,10),(2,5,22),(2,7,22),(3,5,96),(4,5,22),(5,5,85),(6,5,183),(1520,5,54)
--------------
--------------
select * from `test`
--------------
+------------+------------------+---------------+
| product_id | shooper_group_id | product_price |
+------------+------------------+---------------+
| 1 | 5 | 10 |
| 1 | 7 | 10 |
| 2 | 5 | 22 |
| 2 | 7 | 22 |
| 3 | 5 | 96 |
| 4 | 5 | 22 |
| 5 | 5 | 85 |
| 6 | 5 | 183 |
| 1520 | 5 | 54 |
+------------+------------------+---------------+
--------------
select *
from test as t1
where shooper_group_id = 5
and not exists ( select 1
from test as t2
where t2.product_id = t1.product_id
and t2.shooper_group_id = 7
)
--------------
+------------+------------------+---------------+
| product_id | shooper_group_id | product_price |
+------------+------------------+---------------+
| 3 | 5 | 96 |
| 4 | 5 | 22 |
| 5 | 5 | 85 |
| 6 | 5 | 183 |
| 1520 | 5 | 54 |
+------------+------------------+---------------+
--------------
insert into test (`product_id`,`shooper_group_id`,`product_price`)
select product_id, 7 as shooper_group_id, product_price
from test as t1
where shooper_group_id = 5
and not exists ( select 1
from test as t2
where t2.product_id = t1.product_id
and t2.shooper_group_id = 7
)
--------------
--------------
select * from test
--------------
+------------+------------------+---------------+
| product_id | shooper_group_id | product_price |
+------------+------------------+---------------+
| 1 | 5 | 10 |
| 1 | 7 | 10 |
| 2 | 5 | 22 |
| 2 | 7 | 22 |
| 3 | 5 | 96 |
| 3 | 7 | 96 |
| 4 | 5 | 22 |
| 4 | 7 | 22 |
| 5 | 5 | 85 |
| 5 | 7 | 85 |
| 6 | 5 | 183 |
| 6 | 7 | 183 |
| 1520 | 5 | 54 |
| 1520 | 7 | 54 |
+------------+------------------+---------------+
--------------
truncate test
--------------
--------------
insert into `test` (`product_id`,`shooper_group_id`,`product_price`) values
(1,5,10),(1,7,10),(2,5,22),(2,7,22),(3,5,96),(4,5,22),(5,5,85),(6,5,183),(1520,5,54)
--------------
--------------
insert ignore into test (`product_id`,`shooper_group_id`,`product_price`)
select product_id, 7 as shooper_group_id, product_price
from test
where shooper_group_id = 5
--------------
--------------
select * from test
--------------
+------------+------------------+---------------+
| product_id | shooper_group_id | product_price |
+------------+------------------+---------------+
| 1 | 5 | 10 |
| 1 | 7 | 10 |
| 2 | 5 | 22 |
| 2 | 7 | 22 |
| 3 | 5 | 96 |
| 3 | 7 | 96 |
| 4 | 5 | 22 |
| 4 | 7 | 22 |
| 5 | 5 | 85 |
| 5 | 7 | 85 |
| 6 | 5 | 183 |
| 6 | 7 | 183 |
| 1520 | 5 | 54 |
| 1520 | 7 | 54 |
+------------+------------------+---------------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager