1

这是第一个查询:

SELECT 
    SUM(otkupbody_mobile.bruto) AS bruto,
    SUM(otkupbody_mobile.neto) AS neto,
    SUM(otkupbody_mobile.muvla) AS muvla,
    COUNT(*) AS broj_bali
FROM otkupbody_mobile,
     otkupheader_mobile 
WHERE
(
    (otkupheader_mobile.unique_id=otkupbody_mobile.unique_id) 
)

这是第二个:

SELECT
    SUM(otkupbody.bruto) AS bruto,
    SUM(otkupbody.neto) AS neto,
    SUM(otkupbody.muvla) AS muvla,
    COUNT(*) AS broj_bali
FROM otkupbody,
     otkupheader
WHERE
(
    (otkupheader.unique_id=otkupbody.unique_id) 
)

我怎样才能进行一个查询,它会给我两个查询的结果?

4

3 回答 3

2

尝试:

SELECT 
SUM(otkupbody_mobile.bruto) AS bruto,
SUM(otkupbody_mobile.neto) AS neto,
SUM(otkupbody_mobile.muvla) AS muvla,
COUNT(*) AS broj_bali
FROM otkupbody_mobile,
otkupheader_mobile 
WHERE
(
(otkupheader_mobile.unique_id=otkupbody_mobile.unique_id) 
)
UNION ALL
SELECT 
SUM(otkupbody.bruto) AS bruto,
SUM(otkupbody.neto) AS neto,
SUM(otkupbody.muvla) AS muvla,
COUNT(*) AS broj_bali
FROM otkupbody,
otkupheader
WHERE
(
(otkupheader.unique_id=otkupbody.unique_id) 
)

如果您不需要重复,而不是UNION ALL使用普通UNION

于 2012-11-27T11:55:30.693 回答
1

如果您想将合并在一起,您可以简单地使用它。输出将是包含所有提及列的一行

Select  A.*
,   B.*
    From    (
    SELECT  SUM(otkupbody_mobile.bruto) AS bruto,
        SUM(otkupbody_mobile.neto) AS neto,
        SUM(otkupbody_mobile.muvla) AS muvla,
        COUNT(*) AS broj_bali
        FROM otkupbody_mobile,
        otkupheader_mobile 
        WHERE   otkupheader_mobile.unique_id=otkupbody_mobile.unique_id
    )   As  A
    ,   (
    SELECT  SUM(otkupbody.bruto) AS bruto,
        SUM(otkupbody.neto) AS neto,
        SUM(otkupbody.muvla) AS muvla,
        COUNT(*) AS broj_bali
        FROM otkupbody,
        otkupheader
        WHERE
        (
        (otkupheader.unique_id=otkupbody.unique_id) 
        )
    )   As  B

UNION或者,如果要合并行,可以在查询之间使用子句。使用union时应注意两个结果必须具有相同的列。输出将是行,其中包含第一个查询的列。像这样:

    SELECT  SUM(otkupbody_mobile.bruto) AS bruto,
        SUM(otkupbody_mobile.neto) AS neto,
        SUM(otkupbody_mobile.muvla) AS muvla,
        COUNT(*) AS broj_bali
        FROM otkupbody_mobile,
        otkupheader_mobile 
        WHERE   otkupheader_mobile.unique_id=otkupbody_mobile.unique_id
UNION
    SELECT  SUM(otkupbody.bruto) AS bruto,
        SUM(otkupbody.neto) AS neto,
        SUM(otkupbody.muvla) AS muvla,
        COUNT(*) AS broj_bali
        FROM otkupbody,
        otkupheader
        WHERE
        (
        (otkupheader.unique_id=otkupbody.unique_id) 
        )
于 2012-11-27T11:59:35.137 回答
0

使用 UNION ALL。总而言之,对结果使用 SUM。像这样:

select sum(bruto), sum(neto), sum(muvla), sum(broj_bali) from (
  SELECT 
  SUM(otkupbody_mobile.bruto) AS bruto,
  SUM(otkupbody_mobile.neto) AS neto,
  SUM(otkupbody_mobile.muvla) AS muvla,
  COUNT(*) AS broj_bali
  FROM otkupbody_mobile,
  otkupheader_mobile 
  WHERE
  (
  (otkupheader_mobile.unique_id=otkupbody_mobile.unique_id) 
  )
    UNION ALL
  SELECT 
  SUM(otkupbody.bruto) AS bruto,
  SUM(otkupbody.neto) AS neto,
  SUM(otkupbody.muvla) AS muvla,
  COUNT(*) AS broj_bali
  FROM otkupbody,
  otkupheader
  WHERE
  (
  (otkupheader.unique_id=otkupbody.unique_id) 
  )

SQL 小提琴示例

于 2012-11-27T11:57:02.713 回答