如何在sql中编写此查询?

时间:2018-07-23 10:34:49

标签: mysql sql database search

表格:

TEAMS( tid ,tname,tcolor,预算)

玩家( pid ,pname,年龄,国家/地区)

PLAYS( pid tid ,季节,价值)

-(在PLAYERS表中对pid的pid引用)

-(在TEAMS表中对tid的tid引用)

WINS( wtid ltid ,季节,wscore,lscore)

-((TEAMS表中对tid的wtid(赢家)和ltid(失败者)引用)

-

我必须为此编写查询:(我尝试使用“不存在”和“不存在”,但无法做到这一点。)

查找与所有团队一起参加比赛的团队的花絮。

2 个答案:

答案 0 :(得分:0)

这可以做到:

SELECT team
FROM (
    SELECT tid team, WINS.ltid otherteam
    FROM TEAMS
    LEFT JOIN WINS ON WINS.wtid = tid
    UNION ALL 
    SELECT tid team, LOSE.wtid otherteam
    FROM TEAMS
    LEFT JOIN WINS LOSE ON LOSE.ltid = tid
)
GROUP BY team
HAVING COUNT(DISTINCT otherteam) >= (SELECT COUNT(tid)-1 AS teamcount FROM TEAMS)otherteams

答案 1 :(得分:0)

这对我有用。

SELECT 
     t1.tid
FROM
     teams t1
WHERE
     NOT EXISTS( SELECT 
             t2.tid
         FROM
             teams t2
         WHERE
             t2.tid NOT IN (SELECT 
                     wtid
                 FROM
                     wins
                 WHERE
                     ltid = t1.tid UNION (SELECT 
                     ltid
                 FROM
                     wins
                 WHERE
                     wtid = t1.tid)) AND t1.tid != t2.tid)