2012-05-13 97 views
0
$query = "SELECT DISTINCT t.task_id, e.url,e.error_id, p.page_rank, e.scan_status, e.url_is_route,e.url_is_route,e.forbidden_word,e.google_host_fail,e.google_cache_fail,e.google_title_fail,e.robots_noindex_nofollow,e.xrobots_noindex_nofollow,e.title_fetch_warn,e.h1_fail,e.h2_fail,e.h3_fail,e.h1_warn,e.h2_warn,e.h3_warn 
       FROM `error_report` AS e 
       INNER JOIN task_pages AS t ON t.task_id=e.task_id 
       INNER JOIN `pages` AS p ON p.page_id=t.page_id 
       WHERE t.task_id=" . $this->task_id ; 

我希望查詢只能通過t.task_id區分。問題是,當我添加更多的字段時,查詢不再有區別。是否還有一種方法僅通過t.task_id選擇不同的?的mysql distinct statement not working

回答

3

代替distinct使用group by

$query = "SELECT t.task_id, e.url,e.error_id, p.page_rank, e.scan_status, e.url_is_route,e.url_is_route,e.forbidden_word,e.google_host_fail,e.google_cache_fail,e.google_title_fail,e.robots_noindex_nofollow,e.xrobots_noindex_nofollow,e.title_fetch_warn,e.h1_fail,e.h2_fail,e.h3_fail,e.h1_warn,e.h2_warn,e.h3_warn 
      FROM `error_report` AS e 
      INNER JOIN task_pages AS t ON t.task_id=e.task_id 
      INNER JOIN `pages` AS p ON p.page_id=t.page_id 
      WHERE t.task_id=" . $this->task_id 
      ."group by t.task_id"; 
+0

很好的解決方案..謝謝.... –

1

如果你添加更多的字段,你應該使用IIRC GROUP BY

請注意,您的額外字段應爲爲集合,例如, MINMAX等.MySQL更爲寬容,並且顯示了每個領域的第一個價值(雖然不是很明顯)。

+0

,但我沒有使用最小,最大...等 –

+0

@DmitryMakovetskiyd這很好,但您的查詢基本上不能移植到任何其他數據庫引擎;-) –