PHP mysql duplicated rows


PHP mysql duplicated rows

我在MySQL中使用php进行了一个查询。查询低于

$sql = "select m.id, s_1.username player_1, s_2.username player_2, current_step, won
from {$table_prefix} match m left join {$table_prefix}session s_1 on s_1.id = player_1
left join {$table_prefix}session s_2 on s_2.id = player_2
where current_step < 10";

问题是,我的查询工作得很好,但当它需要快速(背靠背)执行时,我会得到重复的行。我该如何防止重复行。

谢谢。

您需要使用DISTINCT来只获取唯一的返回行。

$sql = "select DISTINCT m.id, s_1.username player_1, s_2.username player_2, current_step, won from {$table_prefix}match m left join {$table_prefix}session s_1 on s_1.id = player_1 left join {$table_prefix}session s_2 on s_2.id = player_2 where current_step < 10";

http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html