1

この問題で私を助けることができますか?これら 3 つの SELECT を結合する必要があります.........可能ですか? テーブルが 1 つ必要です... collums:jmeno、Pocet_zapasu、branek、Pocet_zapasu2、branek2、Pocet_turnaju、branek3。

/*Soutezni zapasy*/
SELECT
    Jmeno,
    COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Pocet_zapasu,
    SUM(CASE WHEN Branky>0 then Branky end) as branek
FROM Hraci
INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace 
INNER JOIN Zapas ON Zapas.ID_zapas=Ucast_Zapas.ID_zapasu_ucast 
INNER JOIN Kategorie ON Kategorie.IDkategorie=Zapas.Kategorie_zapas 
WHERE (Kategorie.IDkategorie = 1) AND Zapas_Datum >= '1/1/2013' AND Zapas_Datum < '9/1/2014' AND(Zapas.Druh=1)
GROUP BY Jmeno;

/*Pratelske zapasy*/
SELECT
    Jmeno,
    COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Pocet_zapasu2,
    SUM(CASE WHEN Branky>0 then Branky end) as branek2
FROM Hraci
INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace 
INNER JOIN Zapas ON Zapas.ID_zapas=Ucast_Zapas.ID_zapasu_ucast 
INNER JOIN Kategorie ON Kategorie.IDkategorie=Zapas.Kategorie_zapas 
WHERE (Kategorie.IDkategorie = 1) AND Zapas_Datum >= '1/1/2013' AND Zapas_Datum < '9/1/2014' AND(Zapas.Druh=2)
GROUP BY Jmeno;

/*turnaje*/
SELECT
    Jmeno,
    COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Pocet_turnaju,
    SUM(CASE WHEN Branky>0 then Branky end) as branek3
FROM Hraci
INNER JOIN Ucast_Turnaj ON Ucast_Turnaj.ID_Hrace_turnaj=Hraci.IDHrace 
INNER JOIN Turnaj ON Turnaj.ID_turnaj=Ucast_Turnaj.ID_turnaje_ucast 
INNER JOIN Kategorie ON Kategorie.IDkategorie=Turnaj.Kategorie_Turnaj 
WHERE (Kategorie.IDkategorie = 1) AND Turnaj_Datum >= '1/1/2013' AND Turnaj_Datum < '9/1/2014'
GROUP BY Jmeno;
4

4 に答える 4

1

CASE最初の 2 つのクエリを組み合わせて、ステートメントに add を使用するだけでよいと思います。あなたの 3 番目についてはよくわからないので、結合しようとする代わりに、サブクエリとして追加し、 a を使用しLEFT JOINました。

SELECT
    Jmeno,
    COUNT(CASE WHEN Ucast = 'TRUE' AND Zapas.Druh=1 then 1 end) as Pocet_zapasu,
    SUM(CASE WHEN Branky> 0AND Zapas.Druh=1 then Branky end) as branek,
    COUNT(CASE WHEN Ucast = 'TRUE' AND Zapas.Druh=2 then 1 end) as Pocet_zapasu2,
    SUM(CASE WHEN Branky>0 AND Zapas.Druh=2 then Branky end) as branek2,
    Pocet_turnaju,
    branek3
FROM Hraci
    INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace 
    INNER JOIN Zapas ON Zapas.ID_zapas=Ucast_Zapas.ID_zapasu_ucast 
    INNER JOIN Kategorie ON Kategorie.IDkategorie=Zapas.Kategorie_zapas 
    LEFT JOIN (
        SELECT
            Jmeno,
            COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Pocet_turnaju
            SUM(CASE WHEN Branky>0 then Branky end) as branek3
        FROM Hraci
            INNER JOIN Ucast_Turnaj ON Ucast_Turnaj.ID_Hrace_turnaj=Hraci.IDHrace 
            INNER JOIN Turnaj ON Turnaj.ID_turnaj=Ucast_Turnaj.ID_turnaje_ucast 
            INNER JOIN Kategorie ON Kategorie.IDkategorie=Turnaj.Kategorie_Turnaj 
        WHERE (Kategorie.IDkategorie = 1) AND Turnaj_Datum >= '1/1/2013' AND Turnaj_Datum < '9/1/2014'
        GROUP BY Jmeno
    ) t ON Hraci.Jmeno = t.Jmeno
WHERE (Kategorie.IDkategorie = 1) 
    AND Zapas_Datum >= '1/1/2013' 
    AND Zapas_Datum < '9/1/2014' 
    AND Zapas.Druh IN (1,2)
GROUP BY Jmeno;

テーブル構造を完全に理解していなくても、これをさらに統合できる場合があります。また、どのテーブルから Jmeno を引っ張ってきたのかわかりませんでした -- 私は Hraci だと思いました。

あなたの質問に対する私の理解から、UNION追加の列は作成されず、追加の行だけが作成されるため、使用したくありません。たとえばSELECT 1 as Col1 UNION SELECT 2、2 つの行を含む 1 つの列を返します。

于 2013-04-05T13:54:52.897 に答える
0

各 SQL クエリの間に UNION ALL を配置する必要があります。UNION ALL を配置すると、各クエリの結果が得られ、一緒に表示されます。これが機能するには、各クエリに同じ数の列と列名が必要です。

編集:列名を同じになるように編集しました。編集:クエリを壊していたグループからセミコロンを削除しました。

SELECT
    Jmeno as Column1,
    COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Column2,
    SUM(CASE WHEN Branky>0 then Branky end) as Column3
FROM Hraci
INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace 
INNER JOIN Zapas ON Zapas.ID_zapas=Ucast_Zapas.ID_zapasu_ucast 
INNER JOIN Kategorie ON Kategorie.IDkategorie=Zapas.Kategorie_zapas 
WHERE (Kategorie.IDkategorie = 1) AND Zapas_Datum >= '1/1/2013' AND Zapas_Datum < '9/1/2014' AND(Zapas.Druh=1)
GROUP BY Jmeno

UNION ALL

SELECT
    Jmeno as Column1,
    COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Column2,
    SUM(CASE WHEN Branky>0 then Branky end) as Column3
FROM Hraci
INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace 
INNER JOIN Zapas ON Zapas.ID_zapas=Ucast_Zapas.ID_zapasu_ucast 
INNER JOIN Kategorie ON Kategorie.IDkategorie=Zapas.Kategorie_zapas 
WHERE (Kategorie.IDkategorie = 1) AND Zapas_Datum >= '1/1/2013' AND Zapas_Datum < '9/1/2014' AND(Zapas.Druh=2)
GROUP BY Jmeno

UNION ALL

SELECT
    Jmeno as Column1,
    COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Column2,
    SUM(CASE WHEN Branky>0 then Branky end) as Column3
FROM Hraci
INNER JOIN Ucast_Turnaj ON Ucast_Turnaj.ID_Hrace_turnaj=Hraci.IDHrace 
INNER JOIN Turnaj ON Turnaj.ID_turnaj=Ucast_Turnaj.ID_turnaje_ucast 
INNER JOIN Kategorie ON Kategorie.IDkategorie=Turnaj.Kategorie_Turnaj 
WHERE (Kategorie.IDkategorie = 1) AND Turnaj_Datum >= '1/1/2013' AND Turnaj_Datum < '9/1/2014'
GROUP BY Jmeno
于 2013-04-05T13:53:00.090 に答える
0
SELECT * FROM   
((SELECT
    Jmeno,
    COUNT(CASE WHEN Ucast = 'TRUE' THEN 1 END) AS Pocet_zapasu,
    SUM(CASE WHEN Branky>0 THEN Branky END) AS branek
FROM Hraci
INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace 
INNER JOIN Zapas ON Zapas.ID_zapas=Ucast_Zapas.ID_zapasu_ucast 
INNER JOIN Kategorie ON Kategorie.IDkategorie=Zapas.Kategorie_zapas 
WHERE (Kategorie.IDkategorie = 1) AND Zapas_Datum >= '1/1/2013' AND Zapas_Datum < '9/1/2014' AND(Zapas.Druh=1)
GROUP BY Jmeno
) AS a CROSS JOIN



(SELECT   
    COUNT(CASE WHEN Ucast = 'TRUE' THEN 1 END) AS Pocet_zapasu2,
    SUM(CASE WHEN Branky>0 THEN Branky END) AS branek2
FROM Hraci
INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace 
INNER JOIN Zapas ON Zapas.ID_zapas=Ucast_Zapas.ID_zapasu_ucast 
INNER JOIN Kategorie ON Kategorie.IDkategorie=Zapas.Kategorie_zapas 
WHERE (Kategorie.IDkategorie = 1) AND Zapas_Datum >= '1/1/2013' AND Zapas_Datum < '9/1/2014' AND(Zapas.Druh=2)
GROUP BY Jmeno
) AS b CROSS JOIN



(SELECT           
    COUNT(CASE WHEN Ucast = 'TRUE' THEN 1 END) AS Pocet_turnaju,
    SUM(CASE WHEN Branky>0 THEN Branky END) AS branek3
FROM Hraci
INNER JOIN Ucast_Turnaj ON Ucast_Turnaj.ID_Hrace_turnaj=Hraci.IDHrace 
INNER JOIN Turnaj ON Turnaj.ID_turnaj=Ucast_Turnaj.ID_turnaje_ucast 
INNER JOIN Kategorie ON Kategorie.IDkategorie=Turnaj.Kategorie_Turnaj 
WHERE (Kategorie.IDkategorie = 1) AND Turnaj_Datum >= '1/1/2013' AND Turnaj_Datum < '9/1/2014'
GROUP BY Jmeno
) AS c)

mySQL でテストしたところ、エラーが解消されるはずです

于 2013-04-05T13:49:44.550 に答える
0
select A.jmeno, A.Pocet_zapasu,A.branek,B.Pocet_zapasu2,B.branek2,C.Pocet_turnaju,C.branek3
FROM
    (SELECT
        Jmeno,
        COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Pocet_zapasu,
        SUM(CASE WHEN Branky>0 then Branky end) as branek
    FROM Hraci
    INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace 
    INNER JOIN Zapas ON Zapas.ID_zapas=Ucast_Zapas.ID_zapasu_ucast 
    INNER JOIN Kategorie ON Kategorie.IDkategorie=Zapas.Kategorie_zapas 
    WHERE (Kategorie.IDkategorie = 1) AND Zapas_Datum >= '1/1/2013' AND Zapas_Datum < '9/1/2014' AND(Zapas.Druh=1)
    GROUP BY Jmeno
    ) a
INNER JOIN  
    /*Pratelske zapasy*/
    (SELECT
        Jmeno,
        COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Pocet_zapasu2,
        SUM(CASE WHEN Branky>0 then Branky end) as branek2
    FROM Hraci
    INNER JOIN Ucast_Zapas ON Ucast_Zapas.ID_Hrace_zapas=Hraci.IDHrace 
    INNER JOIN Zapas ON Zapas.ID_zapas=Ucast_Zapas.ID_zapasu_ucast 
    INNER JOIN Kategorie ON Kategorie.IDkategorie=Zapas.Kategorie_zapas 
    WHERE (Kategorie.IDkategorie = 1) AND Zapas_Datum >= '1/1/2013' AND Zapas_Datum < '9/1/2014' AND(Zapas.Druh=2)
    GROUP BY Jmeno
    ) B
    ON A.Jmeno = B.Jmeno
INNER JOIN 
    (/*turnaje*/
    SELECT
        Jmeno,
        COUNT(CASE WHEN Ucast = 'TRUE' then 1 end) as Pocet_turnaju,
        SUM(CASE WHEN Branky>0 then Branky end) as branek3
    FROM Hraci
    INNER JOIN Ucast_Turnaj ON Ucast_Turnaj.ID_Hrace_turnaj=Hraci.IDHrace 
    INNER JOIN Turnaj ON Turnaj.ID_turnaj=Ucast_Turnaj.ID_turnaje_ucast 
    INNER JOIN Kategorie ON Kategorie.IDkategorie=Turnaj.Kategorie_Turnaj 
    WHERE (Kategorie.IDkategorie = 1) AND Turnaj_Datum >= '1/1/2013' AND Turnaj_Datum < '9/1/2014'
    GROUP BY Jmeno
    ) C
ON A.Jmeno = C.Jmeno
于 2013-04-05T14:23:54.620 に答える