如何将3个SQL查询组合成1个?

时间:2016-09-09 08:48:26

标签: sql oracle

我将产品存储在3个表中,产品价格存储在3个表中。目前我运行每个SQL查询,然后在Microsoft Excel中手动合并数据,但是可以合并这些查询,以便我可以在一次点击中获得所有结果吗?

我在考虑使用3个子查询,但我不确定这是否是正确的方法。

1。查询

<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>

2。查询

SELECT
  kust_adr.KU_NAME AS "Customer Name",
  lzr_daten.LZR_BEZ AS "Product Name",
  lzr_przu.LZR_PR AS "Price"
FROM kust_adr,
     lzr_daten,
     lzr_przu
WHERE lzr_przu.LZR_KUNR = kust_adr.KU_NR
AND lzr_daten.LZR_IDNR =
lzr_przu.LZR_IDNR
AND (lzr_daten.LZR_IDNR IN (85)
AND kust_adr.KU_ADR_ART = 0)

3。查询

SELECT
  kust_adr.KU_NAME AS "Customer Name",
  glas_daten_basis.GL_BEZ AS
  "Product Name",
  os_przu.ZUM2 AS "Price"
FROM glas_daten_basis,
     kust_adr,
     os_przu
WHERE os_przu.KUNR = kust_adr.KU_NR
AND glas_daten_basis.IDNR = os_przu.IDNR
AND (glas_daten_basis.IDNR IN (4, 104, 9, 109, 309, 311)
AND kust_adr.KU_ADR_ART =
0)

1 个答案:

答案 0 :(得分:3)

UNION删除重复的行:

SELECT kust_adr.KU_NAME AS "Customer Name",
       lzr_daten.LZR_BEZ AS "Product Name",
       lzr_przu.LZR_PR AS "Price"
  FROM kust_adr, lzr_daten, lzr_przu
 WHERE     lzr_przu.LZR_KUNR = kust_adr.KU_NR
       AND lzr_daten.LZR_IDNR = lzr_przu.LZR_IDNR
       AND (lzr_daten.LZR_IDNR IN (85) AND kust_adr.KU_ADR_ART = 0)
UNION
SELECT kust_adr.KU_NAME AS "Customer Name",
       glas_daten_basis.GL_BEZ AS "Product Name",
       os_przu.ZUM2 AS "Price"
  FROM glas_daten_basis, kust_adr, os_przu
 WHERE os_przu.KUNR = kust_adr.KU_NR AND glas_daten_basis.IDNR = os_przu.IDNR
       AND (glas_daten_basis.IDNR IN (4, 104, 9, 109, 309, 311)
            AND kust_adr.KU_ADR_ART = 0)
UNION
SELECT kust_adr.KU_NAME AS "Customer Name",
       gas_daten.GAS_BEZ AS "Product Name",
       gas_przu.GAS_MIN_M2 AS "Price"
  FROM kust_adr, gas_daten, gas_przu
 WHERE     gas_przu.GAS_KUNR = kust_adr.KU_NR
       AND gas_daten.GAS_IDNR = gas_przu.GAS_IDNR
       AND (kust_adr.KU_ADR_ART = 0);

UNION ALL不会删除重复的行:

SELECT kust_adr.KU_NAME AS "Customer Name",
       lzr_daten.LZR_BEZ AS "Product Name",
       lzr_przu.LZR_PR AS "Price"
  FROM kust_adr, lzr_daten, lzr_przu
 WHERE     lzr_przu.LZR_KUNR = kust_adr.KU_NR
       AND lzr_daten.LZR_IDNR = lzr_przu.LZR_IDNR
       AND (lzr_daten.LZR_IDNR IN (85) AND kust_adr.KU_ADR_ART = 0)
UNION ALL
SELECT kust_adr.KU_NAME AS "Customer Name",
       glas_daten_basis.GL_BEZ AS "Product Name",
       os_przu.ZUM2 AS "Price"
  FROM glas_daten_basis, kust_adr, os_przu
 WHERE os_przu.KUNR = kust_adr.KU_NR AND glas_daten_basis.IDNR = os_przu.IDNR
       AND (glas_daten_basis.IDNR IN (4, 104, 9, 109, 309, 311)
            AND kust_adr.KU_ADR_ART = 0)
UNION ALL
SELECT kust_adr.KU_NAME AS "Customer Name",
       gas_daten.GAS_BEZ AS "Product Name",
       gas_przu.GAS_MIN_M2 AS "Price"
  FROM kust_adr, gas_daten, gas_przu
 WHERE     gas_przu.GAS_KUNR = kust_adr.KU_NR
       AND gas_daten.GAS_IDNR = gas_przu.GAS_IDNR
       AND (kust_adr.KU_ADR_ART = 0);