获取所有唯一记录及其对应的列 [英] get all unique records and their corresponding column

查看:58
本文介绍了获取所有唯一记录及其对应的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在数据库中获取所有唯一记录及其对应的列?

Is it possible to get all unique records as well as their corresponding column in a database?

类似:

SELECT DISTINCT *
FROM table_name
?where?

我想获取所有唯一记录及其对应的列.

I want to get all unique records with their corresponding column.

我试过了:

SELECT distinct(column_name), other_column
FROM table_name
?where?

我仍然收到重复的记录.

I still get duplicate records.

我试过了:

SELECT distinct(column_name)
    FROM table_name
    ?where?

我得到唯一记录但不完整的列.如何获取所有带有列的唯一记录?

I get unique records but incomplete column. How can I get all unique records w/ their column?

推荐答案

您在寻找这样的东西吗?

Are you looking for something like this?

SELECT t.*
  FROM
(
  SELECT MIN(pk_id) pk_id
    FROM table_name
   GROUP BY fk_id
) q JOIN table_name t
    ON q.pk_id = t.pk_id

这是SQLFiddle演示

Here is SQLFiddle demo

在 Postgres 中你可以使用 DISTINCT ON

In Postgres you can use DISTINCT ON

SELECT DISTINCT ON (fk_id) t.*
  FROM table_name t
 ORDER BY fk_id

这是SQLFiddle 演示

Here is SQLFiddle demo

这篇关于获取所有唯一记录及其对应的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆