在我的表中,我有CategoryID和ProductName,这里CategoryID具有重复的值。如何选择具有不同类别ID的product名称?
我已经尝试了看起来类似的堆栈溢出答案,但是都没有帮助。
+++++++++++++++ ++++++++++++++
+ ProductName + + CategoryID +
+++++++++++++++ ++++++++++++++
Mac 1
HP 3
Walker 1
Bell 2
Dell 4
Lenovo 3
Pixel 2
结果应该是
+++++++++++++++ ++++++++++++++
+ ProductName + + CategoryID +
+++++++++++++++ ++++++++++++++
Mac 1
HP 3
Bell 2
Dell 4
分析解答
您只需要group by categoryid
并获得最小(或最大)productname
:
select categoryid, min(productname) as productname
from tablename
group by categoryid