有三个表,如下
Table 1
c_id User_id code
-------------------------
001 UI1 AB01
002 UI2 XD01
003 UI3 AD01
004 UI4 OP01
005 UI5 QW01Table 2
id c_id brn_code sts
-----------------------------------
1 004 90E1 Y
2 005 91E3 Y
3 001 91S4 Y
4 003 93S6 Y
5 002 99S7 YTable 3
Key brn_code
--------------------
1 91S4
2 90D1
3 90Z5
4 93S6
5 96W4需要如下输出
INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('004', '90D1', 'Y');
INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('004', '90Z5', 'Y');
INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('004', '96W4', 'Y');将查询写入为
SELECT 'INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('||t1.c_id||', '||t3.brn_code||', '||''''||'Y'||''''||');'
FROM Table 1 t1 JOIN Table 2 t2
ON t1.c_id = t2.c_id JOIN
(SELECT brn_code FROM Table 3 t3
MINUS
(SELECT brn_code FROM Table 2 t2
WHERE t2.c_id IN (SELECT t1.c_id FROM Table 1 t1 WHERE t1.User_id = 'UI4' AND t1.code = 'OP01')
AND t2.sts = 'Y')) ON t2.brn_code = t3.brn_code;对于上面的查询,我将error作为invalid identifier返回。我不知道为什么会出现这个错误。如何获得如上的输出。
发布于 2020-12-06 01:55:08
您对另一个答案中已经提到的表名有问题。
此外,您还可以通过使用EXISTS简化查询,如下所示:
SELECT 'INSERT INTO table2 t2 (c_id, brn_code, sts)VALUES ('||t1.c_id||', '||t3.brn_code||', '||''''||'Y'||''''||');'
FROM Table1 t1 JOIN Table2 t2 ON t1.c_id = t2.c_id
JOIN table3 t3 on t2.brn_code = t3.brn_code
Where not exists (
Select 1 from table2 t22 join table1 t11 on t11.c_id = t22.c_id
Where t11.User_id = 'UI4' AND t11.code = 'OP01' AND t22.sts = 'Y' And t3.brn_code = t22.brn_code);https://stackoverflow.com/questions/65158314
复制相似问题