2012-07-20 72 views
0

我在使用php的MySQL中有一個查詢。查詢低於PHP mysql重複行

$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"; 

我有查詢的問題的偉大工程,但是,當它需要快速執行(背靠背)我就要重複行。我將如何去防止重複的行。

謝謝。

+1

重複什麼樣的*,在一個查詢結果相同的行? – xiaowl 2012-07-20 15:22:20

+0

數據庫中是否存在重複行,或者只是在對查詢的響應中?你在一個查詢中獲取重複項,還是從多個應該返回不同結果的查詢中獲取重複項? – octern 2012-07-20 15:36:51

回答

0

您將需要使用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