I have the following tables with data.
Table_1
ID, GROUP_ID, ARTIFACT_ID, VERSION
101, com.abc, pqra, 1.0.0
102, com.abc, pqrb, 2.0.0
103, com.abc, pqrc, 3.0.0
104, com.abc, pqrd, 4.0.0
Table_2
ID, MODULE_ID, ISSUE_KEY
11, 104, XYZ-12
12, 104, XYZ-34
I am using the following command to insert values in Table_2.
INSERT INTO Table_2 (MODULE_ID, ISSUE_KEY) SELECT ID, 'XYZ-56' FROM Table_1
WHERE Table_1.GROUP_ID = 'com.abc' AND Table_1.ARTIFACT_ID = 'pqrd' AND
Table_1.VERSION = '4.0.0'
As you can infer from the above query, I am first trying to get the ID
from TABLE_1 for a given GROUP_ID
, ARTIFACT_ID
and VERSION
value and then inserting data into TABLE_2 with the retrieved ID
and ISSUE_KEY
value as XYZ-56
.
After executing the above command, the Table_2 will look like
ID, MODULE_ID, ISSUE_KEY
11, 104, XYZ-12
12, 104, XYZ-34
13, 104, XYZ-56
The problem with the above INSERT
query is that it will not check whether there already exists a given row with a certain value of MODULE_ID
and ISSUE_KEY
. For example, If I execute the same insert query again, then the table will look like
ID, MODULE_ID, ISSUE_KEY
11, 104, XYZ-12
12, 104, XYZ-34
13, 104, XYZ-56
14, 104, XYZ-56
I want the row to be inserted in Table_2 only of there is no row already present with a given MODULE_ID
and ISSUE_KEY
.
Can someone help me modify the above insert query to do as expected.