我在MySQL表上使用DISTINCT发现了一个神秘的行为,但无法弄清楚:
SELECT DISTINCT `deal_hash`,`city_name`
FROM `a`
WHERE `city_name` = 'b'
...将在 deal_hash 上显示DISTINCT所需的输出。我还可以在select中添加任何其他列,它只会在两种情况下起作用DISTINCT将失败
SELECT DISTINCT `deal_hash`,`deal_link`
FROM `a`
WHERE `city_name` = 'b'
和
SELECT DISTINCT `deal_hash`,`loaded_at`
FROM `a`
WHERE `city_name` = 'b'
deal_link
是varchar(255),loaded_at是INT(20)。
答案 0 :(得分:3)
DISTINCT
显示不同的 行 (列值)。
PostgreSQL是我所知道的唯一支持DISTINCT ON的数据库,应用于特定列。
答案 1 :(得分:2)
select distinct
select
s distinct
行。它不是特定于以下列。
请尝试使用group by
:
select deal_hash, min(deal_link)
from a
where city_name = 'b'
group by deal_hash
或
select deal_hash, max(loaded_at)
from a
where city_name = 'b'
group by deal_hash