select username,max(字段1) as 字段1,max(字段2) as 字段2... from user group by username
create table a(name varchar(10), scot int) insert a select 'a',1 union all select 'b',1 union all select 'c',1 union all select 'd',2 union all select 'e',2select * from a t where [name] = (select max([name]) from a where t.scot=scot)
假设-- 表 :user 字段:namename aa aa bb ff得到 name aa bb ff 不知道LZ是不是这么意思。如果是的话sql如下: Select * from user as a where not exists (select * from yonghu where a.name=user.name and a.id<user.id )
select u.* from user u where username not in(select username from user group by username having count(*)>1)
declare @a table (name varchar(10), scot int) insert @a select 'a',1 union all select 'a',1 union all select 'b',1 union all select 'c',2 union all select 'e',2 select * from @a a where (select count(1) from @a where name=a.name)=1(5 行受影响) name scot ---------- ----------- b 1 c 2 e 2(3 行受影响)
from user
group by username
insert a
select 'a',1 union all
select 'b',1 union all
select 'c',1 union all
select 'd',2 union all
select 'e',2select * from a t where [name] = (select max([name]) from a where t.scot=scot)
表 :user
字段:namename
aa
aa
bb
ff得到
name
aa
bb
ff
不知道LZ是不是这么意思。如果是的话sql如下:
Select * from user as a where not exists
(select * from yonghu where a.name=user.name and a.id<user.id )
u.*
from
user u
where
username not in(select username from user group by username having count(*)>1)
insert @a
select 'a',1 union all
select 'a',1 union all
select 'b',1 union all
select 'c',2 union all
select 'e',2
select * from @a a
where (select count(1) from @a where name=a.name)=1(5 行受影响)
name scot
---------- -----------
b 1
c 2
e 2(3 行受影响)