1
我有兩個表:如何通過連接其他表來插入到一個表中?
table1
id | mcc | mnc | active | client_id
1 202 05 1 4
2 202 06 0 4
.......a lot
table2
id | mcc | mnc | rejectReason
1 202 05 null
2 202 06 null
需要插入到表2,但需要檢查每個表1的項目爲表2是這樣的插入:
SELECT table1 t1
(CASE WHEN t2.id != 0
THEN
INSERT INTO table2 t22
SET (
t22.rejectReason = CONCAT('LOSS OF COVERAGE'),
t22.mcc = t1.mcc,
t22.mnc = t1.mnc,
)
WHERE t22.mcc = t1.mcc
AND t22.mnc = t1.mnc
ELSE
''
END)
LEFT JOIN table2 t2
ON t2.mcc = t1.mcc
AND t2.mnc = t1.mnc
如果table1中還沒有這個項目( mcc/mnc)like table2,然後爲table2插入當前項目。請幫助
RESULT:
table1
id | mcc | mnc | active | client_id
1 202 05 1 4
2 202 06 0 4
3 214 0 1 5
.....
212 16 // not exist
214 07 // not exist
.......a lot
table2
id | mcc | mnc | rejectReason
1 202 05 null
2 202 06 null
3 212 16 LOSS OF COVERAGE // then insert
4 214 07 LOSS OF COVERAGE // then insert
對不起,我困惑,我需要插入,我編輯=/ – axon
你究竟想要發生什麼?請提供預期的輸出。 – sagi
我添加了結果,我需要我認爲 – axon