选择行但忽略单元格值的重复
Selecting rows but disregarding repetition of cell values
我有 table 名重复姓氏和名字的客户。我不知道我应该使用哪种类型的 select 查询来显示关于相同条目的特定行。
示例:
Table: customers
customer_id|firstname|lastname
-----------+---------+---------
0000000001 |Peter |Griffin
0000000002 |Peter |Pan
0000000003 |Mary |Magdalene
0000000003 |Mary |Jane
我想要的输出是:
customer_id|firstname|lastname
-----------+---------+---------
0000000001 |Peter |Griffin
0000000003 |Mary |Magdalene
我知道这很简单,但我是 SQL 的新手。
一种方法是使用 DISTINCT ON
:
SELECT DISTINCT ON (firstname)
customer_id, firstname, lastname
FROM customers
ORDER BY firstname, customer_id;
参见:
- Select first row in each GROUP BY group?
我有 table 名重复姓氏和名字的客户。我不知道我应该使用哪种类型的 select 查询来显示关于相同条目的特定行。
示例:
Table: customers
customer_id|firstname|lastname
-----------+---------+---------
0000000001 |Peter |Griffin
0000000002 |Peter |Pan
0000000003 |Mary |Magdalene
0000000003 |Mary |Jane
我想要的输出是:
customer_id|firstname|lastname
-----------+---------+---------
0000000001 |Peter |Griffin
0000000003 |Mary |Magdalene
我知道这很简单,但我是 SQL 的新手。
一种方法是使用 DISTINCT ON
:
SELECT DISTINCT ON (firstname)
customer_id, firstname, lastname
FROM customers
ORDER BY firstname, customer_id;
参见:
- Select first row in each GROUP BY group?