第一种: Distinct
SELECT Distinct列名称 FROM 表名称
e.g.
SELECT Distinct userid FROM UserInfo;
但是Distinct只能放在字段的开头,如果多个字段,将不会去重。
SELECT Distinct userid, id FROM UserInfo;
第二种,row_number() over() 函数
SELECT top 10 * FROM ( SELECT * FROM ( SELECT a,b,c,d, ROW_NUMBER () OVER ( Partition BY a ORDER BY b ) AS row FROM tab_name1 ) p WHERE p.row =1 ) as aa LEFT JOIN tab_name2 b ON aa.xx = b.xx
参考: