mysql - SQL 查询 - 将行与选定的列合并 [MySQL]

标签 mysql sql

我有下表:

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/

相关文章:

php - 为什么我的 4 个 PHP 输入中只有 1 个正确写入数据库?

android - 帮助 SQLite 查询

java - 如何修改hibernate生成的SQL查询?

php - 使用 PHP 在 SQL 中搜索第一个字母

MySQL 查询将 0 添加到数据库但日志却另有说明?

mysql - MYSQL组函数使用无效

php - MySql 批量插入

sql - sql server中日期转换是如何完成的

MySQL Coercibility 对表达式 : why both expressions are unicode, 的整理是一个错误?

sql - 在 vba 中使用 ADODB 在 SQL 中命名变量