比较两个SQL查询

时间:2015-03-11 20:10:45

标签: mysql

我有一个关于所有NCAA篮球锦标赛结果的MySQL数据库。我正在看着" hass"并且"没有"大学篮球场,并寻找谁进出的" hass"通过检查NCAA锦标赛的出价来列出一段时间。

我有一个查询,计算每个团队在两年内出现的NCAA数量。我想比较两组的结果 - 看看谁退出了谁,谁从一年到下一年下降。

例如,在1985-94赛季期间,哪支球队在10场NCAA锦标赛中取得了6场胜利,在1986-95赛季取得了6场胜利,这两场比赛的差异是什么。这就是我所拥有的:

Select t1.Team AS "1994 Teams",t2.Team AS "1995 Teams" 
FROM
(SELECT Count(DISTINCT TABLE_NAME.`Year`) AS 'Totals', TABLE_NAME.Team, TABLE_NAME.Current_Conference
FROM TABLE_NAME
WHERE TABLE_NAME.`Year` BETWEEN 1985 AND 1994
GROUP BY TABLE_NAME.Team HAVING Totals >= 6
ORDER BY TABLE_NAME.Team) AS t1,

(SELECT Count(DISTINCT TABLE_NAME.`Year`) AS 'Totals', TABLE_NAME.Team, TABLE_NAME.Current_Conference
FROM TABLE_NAME
WHERE TABLE_NAME.`Year` BETWEEN 1986 AND 1995
GROUP BY TABLE_NAME.Team HAVING Totals >= 6
ORDER BY TABLE_NAME.Team) AS t2
WHERE t1.Team = t2.Team

这将返回(在这种情况下)32条记录 - 在1985-94和1986-95两次参加10场NCAA锦标赛中的6支球队。我试图找到一组而不是另一组的球队。

1 个答案:

答案 0 :(得分:0)

执行此操作的一种方法是在WHERE部分中使用子查询:

SELECT t1.Team
  FROM (
           SELECT COUNT(DISTINCT TABLE_NAME.`Year`) AS 'Totals',
                  TABLE_NAME.Team,
                  TABLE_NAME.Current_Conference 
             FROM TABLE_NAME 
            WHERE TABLE_NAME.`Year` BETWEEN 1985 AND 1994 
         GROUP BY TABLE_NAME.Team 
           HAVING Totals >= 6 
         ORDER BY TABLE_NAME.Team
       ) AS t1 
 WHERE t1.team NOT IN (
              SELECT TABLE_NAME.Team 
                FROM TABLE_NAME 
               WHERE TABLE_NAME.`Year` BETWEEN 1986 AND 1995 
            GROUP BY TABLE_NAME.Team 
              HAVING COUNT(DISTINCT TABLE_NAME.`Year`) >= 6 )