The following code aways creates triplicated records, but if I remove the transaction commands, it works as expected, inserting only 1 record for each number on the in
clause:
insert into table1(id2, col)
select col2, 1
from table2
where col2 in (1, 2, 3) and col2 not in (select id2 from table1 where col = 1)
group by col2;
Is this a MySQL bug? The same statement don't cause duplicated records on MSSQL Server.
Is it possible to run this queries or similar ones with transaction on without duplicating records?
Example data before queries:
table1
id | id2 | col
==============
-- empty --
table2
id | col2
==============
1 | 1
2 | 1
3 | 1
4 | 2
5 | 2
6 | 2
7 | 3
8 | 3
9 | 3
Example data after queries:
table1 (* duplicated records I don't want)
id | id2 | col
==============
1 | 1 | 1
2 | 1 | 1 *
3 | 1 | 1 *
4 | 2 | 1
5 | 2 | 1 *
6 | 2 | 1 *
7 | 3 | 1
8 | 3 | 1 *
9 | 3 | 1 *
table2 keeps the same
Obs.:
table2
has duplicated values.insert into() select
ignores distinct
and group by
clauses.Edit:
I found that this problem occurs because MySQL ignores the GROUP BY
clause on an INSERT INTO ... SELECT
query.
Is there a way to group this records on insert?
I found that this problem occurs because MySQL ignores the GROUP BY
clause on an INSERT INTO ... SELECT
query.
Don't know exactly why, but if I use a query that doesn't use GROUP BY
or DISTICT
it works.