mysql vlookup重复值

时间:2014-01-26 11:24:48

标签: mysql select distinct

我想创建mysql查询,该查询将显示此表中的以下结果:

表:

id - projectteam - person - transfered
1 - barcelona - messi - 1
2 - realm - ronaldo - 0
3 - manutd - messi - 0

结果:

player - from - to
messi - barcelona - manutd
ronaldo - null - realm

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

如果每位玩家最多只有一次转移,您可以简单地进行外部自我加入:

SELECT      t.person, f.projectteam `from`, t.projectteam `to`
FROM        my_table t
  LEFT JOIN my_table f
         ON f.person = t.person
        AND f.transfered = 1
WHERE       t.transfered = 0

sqlfiddle上查看。