我有一個名為“Products”的 MySQL 表,其結構如下:
id | name | variety | t_min | t_max | entity
====================================================
1 | hake | salmo salar | -5.27 | 10.3 | entity1
2 | salmon | null | -2.45 | 12.9 | entity1
3 | cod | gadus morhua | -4.98 | 11.98 | entity1
4 | hake | salmo salar | -7.87 | 9.35 | entity1
5 | hake | salmo salar | -2.76 | 8.46 | entity1
我想要的結果是:
id | name | variety | t_min | t_max | entity
====================================================
1 | hake | salmo salar | -5.27 | 10.3 | entity1
2 | salmon | null | -2.45 | 12.9 | entity1
3 | cod | gadus morhua | -4.98 | 11.98 | entity1
我想選擇表的所有列,但在這種情況下避免重復具有相同名稱 品種的行。僅使用 DISTINCT 子句不起作用,因為如果我使用它,我只能選擇我想要唯一的列,那么我該怎么做呢?
uj5u.com熱心網友回復:
使用視窗函式
select *
from (
select *, row_number() over(partition by name, variety order by id) rn
from products
) r
where r.rn = 1
order by r.id
uj5u.com熱心網友回復:
嘗試
select * from ( select * , ROW_NUMBER() OVER(PARTITION BY name ORDER
BY id) AS row from Products ) as a where row = 1
希望它會給你獨特的資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/353216.html
