mysql - How can write this query in sql?
问题描述
Tables:
TEAMS(tid, tname, tcolor, budget)
PLAYERS(pid, pname, age, country)
PLAYS(pid, tid, season, value)
-(pid references to pid in PLAYERS table)
-(tid references to tid in TEAMS table)
WINS(wtid, ltid, season, wscore, lscore)
-(wtid (winner) and ltid (loser) references to tid in TEAMS table)
--
I have to write query for this:(I tried with "not exist" and "not in" but could not do it.)
Find tid of teams that played with all teams.
解决方案
This will do:
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
推荐阅读
- javascript - 在具有相同键的对象中获取乘键值
- reactjs - 我在哪里可以找到最好的教程 react-spring?
- go - 如何处理 NewCBCDecrypter 示例中的无效密钥?
- android - 发生这种情况是因为您使用了不包含您选择的提供者的`BuildContext`
- python - 如何在 Python 中创建实时颜色图?
- python - 调整 QTableView 小部件的大小以消除额外的空白
- r - 提取列表中向量的选定元素
- python - Tkinter 条目小部件插入宽度
- javascript - 使用 HTML 画布将多个蒙版图像叠加在一起
- nodes - [已解决]Godot - get_node:未找到节点 - GDScript