如何仅在该数据可用时将来自另一个 table 的数据附加到串联
How to append data to a concatenation, from another table, only if that data is available
我有 2 个表:
L0INS (ID_INS, Nume_INS)
1 ANB
2 ENEL
3 DIGI
L1AVZ (ID_AVZ, FK_INS, Nume_AVZ)
1 1 APA
2 1 CAN
3 1 HID
4 2 ELE
我想查询显示以下内容:
Query1 (ID_INS, CONCAT(Nume_INS, Nume_AVZ) )
1 ANB, APA
1 ANB, CAN
1 ANB, HID
2 ENEL, ELE
3 DIGI, n/a
所以如果 Table2 中存在具有 L0INS.ID_INS = L1AVZ.FK_INS
的条目,它只会将 Name_T2 添加到串联中
我制作的这个版本确实为每个 Nume_AVZ 附加项显示了一个 L0INS 条目,但也显示了一个包含 'n/a' 的附加项。如果还有另一个条目,我想删除 'n/a' 条目。
SELECT DISTINCT "L0INS"."ID_INS",
COALESCE ( "NUME_INS", 'n/a' ) || ', ' || CASE WHEN "L1AVZ"."FK_INS" = "L0INS"."ID_INS" THEN "L1AVZ"."NUME_AVZ" ELSE 'n/a' END "INS_SELECT",
LOWER ( COALESCE ( "NUME_INS", 'n/a' ) || ', ' || CASE WHEN "L1AVZ"."FK_INS" = "L0INS"."ID_INS" THEN "L1AVZ"."NUME_AVZ" ELSE 'n/a' END ) "INS_SEARCH"
FROM "L1AVZ", "L0INS"
WHERE "L0INS"."ID_INS" IS NOT NULL
我感觉 WHERE 部分缺少某些东西,我无法理解。
LEFT JOIN
不是更简单吗?
SELECT l1."ID_INS",
("NUME_INS" || ', ' || OALESCE(l0."NUME_AVZ", 'n/a') AS INS_SEARCH"
FROM "L1AVZ" l1 LEFT JOIN
"L0INS" lO
ON l1.id_ins = l0.fk_ins;
SELECT L.ID_INS,L.Nume_INS +','+COALESCE(AVZ.Nume_AVZ,'N/A')AS X
FROM L0INS AS L
LEFT JOIN L1AVZ AS AVZ ON L.ID_INS=AVZ.FK_INS
我有 2 个表:
L0INS (ID_INS, Nume_INS)
1 ANB
2 ENEL
3 DIGI
L1AVZ (ID_AVZ, FK_INS, Nume_AVZ)
1 1 APA
2 1 CAN
3 1 HID
4 2 ELE
我想查询显示以下内容:
Query1 (ID_INS, CONCAT(Nume_INS, Nume_AVZ) )
1 ANB, APA
1 ANB, CAN
1 ANB, HID
2 ENEL, ELE
3 DIGI, n/a
所以如果 Table2 中存在具有 L0INS.ID_INS = L1AVZ.FK_INS
的条目,它只会将 Name_T2 添加到串联中我制作的这个版本确实为每个 Nume_AVZ 附加项显示了一个 L0INS 条目,但也显示了一个包含 'n/a' 的附加项。如果还有另一个条目,我想删除 'n/a' 条目。
SELECT DISTINCT "L0INS"."ID_INS",
COALESCE ( "NUME_INS", 'n/a' ) || ', ' || CASE WHEN "L1AVZ"."FK_INS" = "L0INS"."ID_INS" THEN "L1AVZ"."NUME_AVZ" ELSE 'n/a' END "INS_SELECT",
LOWER ( COALESCE ( "NUME_INS", 'n/a' ) || ', ' || CASE WHEN "L1AVZ"."FK_INS" = "L0INS"."ID_INS" THEN "L1AVZ"."NUME_AVZ" ELSE 'n/a' END ) "INS_SEARCH"
FROM "L1AVZ", "L0INS"
WHERE "L0INS"."ID_INS" IS NOT NULL
我感觉 WHERE 部分缺少某些东西,我无法理解。
LEFT JOIN
不是更简单吗?
SELECT l1."ID_INS",
("NUME_INS" || ', ' || OALESCE(l0."NUME_AVZ", 'n/a') AS INS_SEARCH"
FROM "L1AVZ" l1 LEFT JOIN
"L0INS" lO
ON l1.id_ins = l0.fk_ins;
SELECT L.ID_INS,L.Nume_INS +','+COALESCE(AVZ.Nume_AVZ,'N/A')AS X
FROM L0INS AS L
LEFT JOIN L1AVZ AS AVZ ON L.ID_INS=AVZ.FK_INS