如何缩短此查询?

时间:2014-11-22 00:58:04

标签: mysql sql

如何缩短此查询?

我想更改此部分查询,因为它是多余的。

    where leagueid = 2096 and 
              start_time >= 1415938900 and 
             ((matches.radiant_team_id= 1848158 and matches.dire_team_id= 15) 
                 or (matches.radiant_team_id= 15 and matches.dire_team_id= 1848158))

整个查询

    SELECT 
    matches.radiant_name,
    matches.dire_name, 
    TA.Count as teamA,
    TB.Count as teamB, 
    TA.Count + TB.Count AS total_matches,
      (CASE 
            WHEN series_type = 0  THEN 1
        WHEN series_type = 1  THEN 2
          WHEN series_type = 2  THEN 3
      END) as wins_goal
    FROM matches
    LEFT JOIN (SELECT radiant_team_id, COUNT(id) AS Count
    FROM matches

这部分是多余的

    WHERE leagueid = 2096 and 
              start_time >= 1415938900 and 
             ((matches.radiant_team_id= 1848158 and matches.dire_team_id= 15) 
                 or (matches.radiant_team_id= 15 and matches.dire_team_id= 1848158))

这部分是多余的

    GROUP BY radiant_team_id) AS TA ON TA.radiant_team_id = matches.radiant_team_id
    LEFT JOIN (SELECT dire_team_id, COUNT(id) AS Count
             FROM matches

这部分是多余的

    WHERE leagueid = 2096 and 
              start_time >= 1415938900 and 
             ((matches.radiant_team_id= 1848158 and matches.dire_team_id= 15) 
                 or (matches.radiant_team_id= 15 and matches.dire_team_id= 1848158))

这部分是多余的

    GROUP BY dire_team_id) AS TB ON TB.dire_team_id = matches.radiant_team_id

这部分是多余的

    WHERE leagueid = 2096 and 
              start_time >= 1415938900 and 
             ((matches.radiant_team_id= 1848158 and matches.dire_team_id= 15) 
                 or (matches.radiant_team_id= 15 and matches.dire_team_id= 1848158)) 

这部分是多余的

    GROUP BY series_id

2 个答案:

答案 0 :(得分:1)

如果我理解得很好,你应该能够使用临时表来实现它,如下所示:

/* Create temporary table with the set of records that were the cause of query part redundancy */
CREATE TEMPORARY TABLE      
    _tmp_matches
SELECT 
    *
FROM 
    matches
WHERE leagueid = 2096 and 
      start_time >= 1415938900 and 
      ((matches.radiant_team_id= 1848158 and matches.dire_team_id= 15) 
         or (matches.radiant_team_id= 15 and matches.dire_team_id= 1848158));

/* Use the created temporary table */

SELECT 
    _tmp_matches.radiant_name,
    _tmp_matches.dire_name, 
    TA.Count as teamA,
    TB.Count as teamB, 
    TA.Count + TB.Count AS total_matches,
    (CASE 
        WHEN series_type = 0  THEN 1
        WHEN series_type = 1  THEN 2
        WHEN series_type = 2  THEN 3
    END) as wins_goal
FROM _tmp_matches
     LEFT JOIN 
     (  
        SELECT radiant_team_id, COUNT(id) AS Count
        FROM _tmp_matches
        GROUP BY radiant_team_id
    ) AS TA 
    ON TA.radiant_team_id = _tmp_matches.radiant_team_id
    LEFT JOIN 
    (
        SELECT dire_team_id, COUNT(id) AS Count
        FROM _tmp_matches
        GROUP BY dire_team_id
    ) AS TB 
    ON TB.dire_team_id = _tmp_matches.radiant_team_id
GROUP BY 
    series_id;

/* Delete no longer needed temporary table */

DROP TEMPORARY TABLE _tmp_matches;

我希望它对你有所帮助。

答案 1 :(得分:0)

好吧,我在SQL方面不太好,但您可以尝试将冗余语句存储在变量上,然后在需要时调用它。

相关问题