I have 2 table, first I retrieve all Ids like this:
select DISTINCT entity_id From table1
I want to create a bulk insert using entity_id and prevent duplicate if the entity_id have a specifique value in the colomn2:
example
*********************************************
* ID * ENTITY_ID * COLOMN2 * value *
*********************************************
* 1 * 230 * 20 * 1 *
* 2 * 230 * 100 * 1 *
* 3 * 280 * 20 * 0 *
* 4 * 220 * 20 * 1 *
*********************************************
select DISTINCT entity_id From table1 // return : 230,280,220
INSERT into table1 ('ENTITY_ID','COLOMN2','VALUE') VALUES([the select id],100,1)
WHERE COLOMN2<>100
or something like that?
the result need to be:
creating 2 insert... entity_id=220 and entity_id=280
with the colomn2=100 and value = 1
any idea ?
Use a subquery
INSERT INTO table1 ('ENTITY_ID','COLOMN2','VALUE')
(select DISTINCT entity_id ,100,1 FROM table2 WHERE column2<>100)
If I'm understanding you correctly, you consider "duplicate" to mean BOTH "ENTITY_ID" and "COLOMN2" have the same value, ie:
(50, 50) & (50, 99) == not duplicate
(80, 42) & (99, 42) == not duplicate
(55, 66) & (55, 66) == duplicate
Can you create a compound unique key on your database? Such as, for mysql:
CREATE UNIQUE INDEX nodupes ON table1 (ENTITY_ID, COLOMN2);
You would then need to determine how to handle errors during the insert process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.