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) 
)

これは2番目です:

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) 
)

両方のクエリの結果を返す 1 つのクエリを作成するにはどうすればよいですか?

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

をマージする場合は、これを使用できます。出力は、すべての言及列を含む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する場合は、両方の結果の列が同じである必要があることに注意してください。出力は、最初のクエリの列を持つ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
    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

UNIONALLを使用します。要約すると、結果に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 に答える