Dmitry Makovetskiyd
Dmitry Makovetskiyd

Reputation: 7053

mysql distinct statement not working

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

I want the query to be distinct only by t.task_id. The problem is that when I add more fields..the query isnt distinct anymore. Is there still a way to select distinct by t.task_id only?

Upvotes: 0

Views: 143

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173562

If you add more fields, you should use GROUP BY iirc.

Note that your extra fields should be aggregates, e.g. MIN, MAX, etc. MySQL is more forgiving and shows the first value of each field (though not reliably apparently).

Upvotes: 1

juergen d
juergen d

Reputation: 204766

instead of distinct use 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";

Upvotes: 3

Related Questions