1

你能帮我解决这个问题吗?我需要将这三个 SELECT 连接在一起............有可能吗?我需要一张桌子……有 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

我认为你可以结合你的前 2 个查询,然后在你的CASE语句中使用 add 。我不确定你的第三个,所以我没有尝试组合它,而是将它添加为子查询并使用 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 行的单列。

于 2013-04-05T13:54:52.897 回答
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

您需要在每个 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 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 回答