declare @t table (id int,title varchar(2),author varchar(5),[content] varchar(3)) insert into @t select 1,'a1','aaa','sss' union all select 2,'a1','abvcv','sss' union all select 3,'a2','nkjks','sss' union all select 4,'a2','asdf','sss' union all select 5,'a3','aaa','ss'--得到title不重复的记录 select * from @t t where id=( select min(id) from @t where title=t.title) /* id title author content ----------- ----- ------ ------- 1 a1 aaa sss 3 a2 nkjks sss 5 a3 aaa ss */
declare @t table (id int,title varchar(2),author varchar(5),[content] varchar(3)) insert into @t select 1,'a1','aaa','sss' union all select 2,'a1','abvcv','sss' union all select 3,'a2','nkjks','sss' union all select 4,'a2','asdf','sss' union all select 5,'a3','aaa','ss'--统计相同标题的条数 select title,条数=count(1) from @t t group by title /* title 条数 ----- ----------- a1 2 a2 2 a3 1 */
select t.* from tb t where id = (select max(id) from tb where title = t.title)select t.* from tb t where not exists (select 1 from tb where title = t.title and id > t.id)
declare @t table
(id int,title varchar(2),author varchar(5),[content] varchar(3))
insert into @t
select 1,'a1','aaa','sss' union all
select 2,'a1','abvcv','sss' union all
select 3,'a2','nkjks','sss' union all
select 4,'a2','asdf','sss' union all
select 5,'a3','aaa','ss'--得到title不重复的记录
select * from @t t where id=(
select min(id) from @t where title=t.title)
/*
id title author content
----------- ----- ------ -------
1 a1 aaa sss
3 a2 nkjks sss
5 a3 aaa ss
*/
declare @t table
(id int,title varchar(2),author varchar(5),[content] varchar(3))
insert into @t
select 1,'a1','aaa','sss' union all
select 2,'a1','abvcv','sss' union all
select 3,'a2','nkjks','sss' union all
select 4,'a2','asdf','sss' union all
select 5,'a3','aaa','ss'--统计相同标题的条数
select title,条数=count(1) from @t t group by title
/*
title 条数
----- -----------
a1 2
a2 2
a3 1
*/
select t.* from tb t where id = (select max(id) from tb where title = t.title)select t.* from tb t where not exists (select 1 from tb where title = t.title and id > t.id)