我有下表:
Game | Name | Result | Stage 1 A W F 1 B L 0 2 C L F 2 D W 0 3 E L 0 3 F W 0
The output I am looking for:
Game | Name | Result | Stage 1 A W F 2 D W F
I only want to see the winners (W) from the results of stage F.
I can do this via joins (which isn't very fast):
SELECT *
FROM (
SELECT *
FROM MyTable
WHERE Stage = 'F'
) AA
JOIN MyTable
ON AA.Game = MyTable.Game AND AA.Result <> MyTable.Result
..但我想知道是否有更简单、更有效的方法来做到这一点。另外,这需要我之后再做一些过滤。
提前致谢!
最佳答案
要在没有自连接或等价物的情况下执行此类作业,您可能需要使用 MySQL 不支持的 SQL 窗口函数。您使用的联接还不错,但这会更简单一些:
SELECT
players.Game AS Game,
players.Name AS Name,
'W' AS Result,
'F' as Stage
FROM
MyTable stage
JOIN MyTable players
ON stage.Game = players.Game
WHERE
stage.stage = 'F'
AND players.result = 'W'
关于mysql - SQL 查询 - 将行与选定的列合并 [MySQL],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29266849/