表product:
ID Name Category
1 a A
2 b A
3 c B
4 d B
5 e C
6 f C现在希望从Category字段进行分类并选出一条记录显示出来,,
ID Name Category
1 a A
3 c B
5 e C请高手指教!
ID Name Category
1 a A
2 b A
3 c B
4 d B
5 e C
6 f C现在希望从Category字段进行分类并选出一条记录显示出来,,
ID Name Category
1 a A
3 c B
5 e C请高手指教!
not exists(select * from product b where a.Category=b.Category and b.id<a.id)
ID int identity(1,1),
Name char(1),
Category char(1)
)insert product
select 'a','A' union all
select 'b','A' union all
select 'c','B' union all
select 'd','B' union all
select 'e','C' union all
select 'f','C'--第一种方法:
select * from product
select * from product a where ID in(select min(ID) from product where Category=a.Category) --第二种方法:
select a.* from product a,
(select Category,min(ID)as ID from product group by Category)b
where a.Category=b.Category and a.ID=b.ID--结果:
ID Name Category
--------------------------
1 a A
3 c B
5 e C
exists(select * from product b where a.Category=b.Category and a.id<b.id)
where id not in (select max(id)
from product
group by Category)
select * from product where id in (select min(id) from product group by category)
select a.* from product a,
(select Category,min(ID)as ID from product group by Category)b
where a.Category=b.Category
ID int identity(1,1),
Name char(1),
Category char(1)
)insert product
select 'a','A' union all
select 'b','A' union all
select 'c','B' union all
select 'd','B' union all
select 'e','C' union all
select 'f','C'select min(id),min(name),Category from product group by Category