从具有不同标准的一列获取2个值?

时间:2014-02-15 21:13:38

标签: sql select

我有2张牌桌,球员和禁令。

players = id, name

bans = id, type, banned_id, accuser_id

如何在一行中同时选择被禁止和指责的玩家姓名?这是我的错误代码:

SELECT `bans`.`id`, `players`.`name` AS `banned`, `players`.`name` AS `accuser`
FROM `bans`, `players`
WHERE `bans`.`type` = 2;

1 个答案:

答案 0 :(得分:0)

如果您只想获取有关被禁用户的数据

SELECT b.id, p.name as accuser FROM bans b JOIN players p  ON p.id=b.accuser_id WHERE b.type=2

如果您想获取有关禁令信息的所有用户的数据

SELECT * FROM players p LEFT JOIN bans b ON b.accuser_id = p.id