这是第一个查询:
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)
)
如何创建一个可以从两个查询中获得结果的查询?
答案 0 :(得分: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
答案 1 :(得分: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)
)
答案 2 :(得分: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)
)