我假设这phone
是你的伪主键。意思是:它应该使用查询变得唯一。
对于 Postgres,以下将起作用:
SELECT DISTINCT ON (phone)
*
FROM your_table
它为每个电话号码选择任意一行,从而使电话号码保持不同。
也许有一个等价DISTINCT ON (...)
于访问?
如果您id
在表中有一个 REAL UNIQUE 标识符,这将起作用:
SELECT *
FROM your_table
WHERE id IN (
SELECT min(id)
FROM your_table
GROUP BY phone
)
得到它的工作,也许不完美(最快的查询),但它正在工作!
SELECT *
FROM (
SELECT ROW_NUMBER() OVER () AS pseudoid, *
FROM your_table
) x
JOIN (
SELECT MIN(pseudoid) AS pseudoid
FROM (
SELECT ROW_NUMBER() OVER () AS pseudoid, phone
FROM your_table
) z
GROUP BY z.phone
) y
ON x.pseudoid = y.pseudoid
解释:
首先(第 9-10 行):
SELECT ROW_NUMBER() OVER () AS pseudoid, phone
FROM your_table
这会为每一行(加上电话号码)提供一个 pseudo_id。所以我们有重复的条目,但每个条目都有其唯一的伪标识。然后(第 7-12 行):
SELECT MIN(pseudoid) AS pseudoid
FROM (
SELECT ROW_NUMBER() OVER () AS pseudoid, phone
FROM your_table
) z
GROUP BY z.phone
这使我们的电话号码独一无二,并始终选择 MIN(伪)。然后(第 3-4 行):
SELECT ROW_NUMBER() OVER () AS pseudoid, *
FROM your_table
为整个表创建一个伪标识。
然后我们加入这些表:这为每个电话号码提供了具有最小伪 ID 的完整行(+伪 ID)。
小一点(也许更快):
WITH pseudo_id_table AS (SELECT ROW_NUMBER() OVER () AS pseudoid, * FROM your_table)
SELECT *
FROM pseudo_id_table x
JOIN (
SELECT MIN(pseudoid) AS pseudoid
FROM pseudo_id_table
GROUP BY phone
) y
ON x.pseudoid = y.pseudoid