用SQL语句取唯一数据

2025-03-22 23:29:42
推荐回答(5个)
回答1:

如果是唯一数据的,肯定是有约束条件来确认结果的唯一性,肯定会用到where语句。
sql:select * from tablename where id ='10';
解释:如果id是主键或者是不重复字段,那么通过固定的id条件,就可以取出唯一数据。

回答2:

楼上各位写的语句是不对的,标准答案如下:
select distinct 字段D,
(select top 1 字段A from tb where 字段D=A.字段D) as 字段A,
(select top 1 字段B from tb where 字段D=A.字段D) as 字段B,
(select top 1 字段C from tb where 字段D=A.字段D) as 字段C
from tb A

回答3:

select * from tableName as t where
not exists (select 1 from tableName where d=t.d and id
这样读出来的如果重复读出来的是第一个

不明白的,百度Hi我。

回答4:

SELECT 字段A,字段B,字段C
FROM 表
WHERE (字段D IN
(SELECT 字段D
FROM 表
GROUP BY 字段D
HAVING COUNT(*) = 1))

回答5:

select * from TABLE a where a.字段A = (select MIN(字段A) from TABLE where 字段D = a.字段D) order by 字段D asc;