如何删除重复的SQL结果?

时间:2013-03-07 09:26:44

标签: mysql database sql-order-by inner-join duplicate-removal

我从名为 guildmembers

的表中提取访问权限,guildid名称

...来自另一个名为玩家

的表格中的id,level,playerclass

这是我的SQL语句:

SELECT guildmembers.access, guildmembers.guildid, guildmembers.name, players.id, players.level, players.playerclass
FROM guildmembers, players
ORDER BY guildmembers.name, players.playerclass

这就是回报。

这就是我需要的。

任何想法如何做到这一点?

3 个答案:

答案 0 :(得分:3)

您需要做的是加入声明。目前你告诉sql只显示两个表中的所有resutls。您希望仅在匹配时显示结果,为此您使用连接语句。

SELECT guildmembers.access, guildmembers.guildid, guildmembers.name, players.id, players.level, players.playerclass
FROM guildmembers join players on guildmembers.pid=players.id
ORDER BY guildmembers.name, players.playerclass

guildmembers.pidplayers.id是两个表中的公共字段,因此您可以加入这些表格。

http://www.w3schools.com/sql/sql_join.asp

使这更容易理解和理解在两个表中都有一个共同的字段名称。然后你可以使用它来加入它,这更有意义。

希望能帮到你。

答案 1 :(得分:1)

您的SQL语句不正确,请尝试

SELECT guildmembers.access, guildmembers.guildid, guildmembers.name, players.id, 
players.level, players.playerclass from guildmembers left join players on guildmembers.pid = players.id order by guildmembers.name, players.playerclass

答案 2 :(得分:1)

SELECT gm.access as access, gm.guildid as guildid, gm.name as name, 
p.id as id, p.level as level, p.playerclass as playerclass
FROM guildmembers gm LEFT JOIN players p on gm.pid=p.id
ORDER BY gm.name, p.playerclass