id int
field1 nvarchar
field2 nvarchar
field3 bit
字段field1中的值是可以相同的。我现在想对表进行查询,要求返回表中所有字段,但field1字段中相同值的只返回第一行,即返回的结果集里field1中的值是唯一的。
这个有点类似于 select distince field1 from 表1,但它只能返回field1这一列,而我需要返回表中的所有列。
请大家帮帮忙看怎么写?多谢。
3 个解决方案
#1
select min(id),field1,min(field2),min(field3) from 表1 group by field1
#2
select * from 表 as t
where not exists(select * from 表 where field1 = t.field1 and id < t.id)
select * from 表 as t
where id = (select min(id) from 表 where field1 = t.field1)
where not exists(select * from 表 where field1 = t.field1 and id < t.id)
select * from 表 as t
where id = (select min(id) from 表 where field1 = t.field1)
#3
多谢,echiynn(寶琲),问题解决了。:)
#1
select min(id),field1,min(field2),min(field3) from 表1 group by field1
#2
select * from 表 as t
where not exists(select * from 表 where field1 = t.field1 and id < t.id)
select * from 表 as t
where id = (select min(id) from 表 where field1 = t.field1)
where not exists(select * from 表 where field1 = t.field1 and id < t.id)
select * from 表 as t
where id = (select min(id) from 表 where field1 = t.field1)
#3
多谢,echiynn(寶琲),问题解决了。:)