开发者

MySQL DISTINCT won't work when certain columns are SELECTed

开发者 https://www.devze.com 2023-02-24 01:37 出处:网络
I got a mysterious behaviour using DISTINCT on a MySQL table and can\'t figure it out: SELECT DISTINCT `deal_hash`,`city_name`

I got a mysterious behaviour using DISTINCT on a MySQL table and can't figure it out:

SELECT DISTINCT `deal_hash`,`city_name` 
  FROM `a` 
 WHERE `city_name` = 'b'

...will show me the desired output with DISTINCT on deal_hash. I can also add any other column to the select and it will work only in two cases DISTINCT will fail

SELECT DISTINCT `deal_hash`,`deal_link` 
  FROM `a` 
 WHERE `city_name` = 'b'

AND

SELECT DISTINCT `开发者_JS百科deal_hash`,`loaded_at` 
  FROM `a` 
 WHERE `city_name` = 'b'

deal_link is a varchar(255) and loaded_at a INT(20).


DISTINCT shows distinct rows (of column values).

PostgreSQL is the only DB I know of that supports DISTINCT ON, applied to a particular column.


select distinct selects distinct rows. It is not specific to the following column.

Try using group by instead:

select deal_hash, min(deal_link)
from a
where city_name = 'b'
group by deal_hash

or

select deal_hash, max(loaded_at)
from a
where city_name = 'b'
group by deal_hash
0

精彩评论

暂无评论...
验证码 换一张
取 消