I have two tables. One is permission_index
and the other is permission_index_bkp
.
My script is supposed to transfer some rows and data from permission_index
to permission_index_bkp
. However, I don't want to create new rows if the data in all 4 columns is the same.
This is what I am trying to use:
INSERT INTO permission_index_bkp
(b_app, b_perm_type, b_perm_type_id, b_perm_3)
SELECT app, perm_type, perm_type_id, perm_3
FROM permission_index
WHERE app = 'module'
AND perm_type = '1'
AND perm_type_id = '2'
AND perm_3 IS NOT NULL
ON DUPLICATE KEY UPDATE app = app,
perm_type = perm_type,
perm_type_id = perm_type_id,
perm_3 = perm_3;
I've been getting errors like:
1052: Column 'app' in field list is ambiguous (I think I fixed this by changing columns' name in permission_index_bkp).
or
1054: Unknown column 'app' in 'field list'
Could you guys tell me how to fix my query?
Thanks!!
EDIT 1:
This has made the errors disappear, however, rows keep being added in permission_index_bkp
, even though they have the same values:
INSERT INTO permission_index_bkp
(b_app, b_perm_type, b_perm_type_id, b_perm_3)
SELECT app, perm_type, perm_type_id, perm_3
FROM permission_index AS pi
WHERE app = 'module'
AND perm_type = '1'
AND perm_type_id = '2'
AND perm_3 IS NOT NULL
ON DUPLICATE KEY UPDATE b_app = b_app,
b_perm_type = b_perm_type,
b_perm_type_id = b_perm_type_id,
b_perm_3 = b_perm_3;
Though I never used this syntax but after doing some search in google I found that you need to declare an unique key on all four columns of your
permission_index
table to remove duplicate entries.Please check below example:
Output:
table test:
table test2:
Output:
table test:
table test2:
db<fiddle here