1 1 xx dd
2 1 xx dd
3 1 xx dd
4 2 xx dd
5 2 xx dd
6 2 xx dd
7 2 xx dd
CategoriesID的总数不固定,每个分类(CategoriesID相同)只取出前十条记录的Sql语句该如何写?
4 个解决方案
#1
create table A_test( [id] int IDENTITY(1,1) ,CategoriesID int, Title varchar(20),content varchar(10))
---------------------------------
insert into A_test
select
1 , 'xx1', 'dd' union
select 1 , 'xx2', 'dd' union
select 1 , 'xx3' , 'dd' union
select 2 , 'xx4', 'dd' union
select 2 , 'xx5' , 'dd' union
select 2 , 'xx6' , 'dd' union
select 2 , 'xx8' , 'dd'union
select 2 , 'xx9', 'dd' union
select 2 , 'xx10' , 'dd' union
select 2 , 'xx11' , 'dd' union
select 2 , 'xx17' , 'dd' union
select 2 , 'xx27' , 'dd'
----------------------------------------------------
insert into A_test
select
1 , 'xx1', 'dd' union
select 1 , 'xx2', 'dd' union
select 1 , 'xx3' , 'dd' union
select 2 , 'xx4', 'dd' union
select 2 , 'xx5' , 'dd' union
select 2 , 'xx6' , 'dd' union
select 2 , 'xx8' , 'dd'union
select 2 , 'xx9', 'dd' union
select 2 , 'xx10' , 'dd' union
select 2 , 'xx11' , 'dd' union
select 2 , 'xx17' , 'dd' union
select 2 , 'xx27' , 'dd'
-----------------------------------------------
select * ,(select sum(1)from a_test (nolock) where CategoriesID=a.CategoriesID and [id]<=a.[id] ) as Tid
into #tmp
from a_test a(nolock)
select [id],categoriesid,title,content ,tid
from #tmp where tid<=10
order by categoriesid,tid
--drop table #tmp delete from atest
---------test ------------
id categoriesid title content tid
----------- ------------ -------------------- ---------- -----------
1 1 xx1 dd 1
2 1 xx2 dd 2
3 1 xx3 dd 3
8 1 xx1 dd 4
9 1 xx2 dd 5
10 1 xx3 dd 6
20 1 xx1 dd 7
21 1 xx2 dd 8
22 1 xx3 dd 9
32 1 xx1 dd 10
4 2 xx4 dd 1
5 2 xx5 dd 2
6 2 xx6 dd 3
7 2 xx7 dd 4
11 2 xx10 dd 5
12 2 xx11 dd 6
13 2 xx17 dd 7
14 2 xx27 dd 8
15 2 xx4 dd 9
16 2 xx5 dd 10
(影響 20 個資料列)
---------------------------------
insert into A_test
select
1 , 'xx1', 'dd' union
select 1 , 'xx2', 'dd' union
select 1 , 'xx3' , 'dd' union
select 2 , 'xx4', 'dd' union
select 2 , 'xx5' , 'dd' union
select 2 , 'xx6' , 'dd' union
select 2 , 'xx8' , 'dd'union
select 2 , 'xx9', 'dd' union
select 2 , 'xx10' , 'dd' union
select 2 , 'xx11' , 'dd' union
select 2 , 'xx17' , 'dd' union
select 2 , 'xx27' , 'dd'
----------------------------------------------------
insert into A_test
select
1 , 'xx1', 'dd' union
select 1 , 'xx2', 'dd' union
select 1 , 'xx3' , 'dd' union
select 2 , 'xx4', 'dd' union
select 2 , 'xx5' , 'dd' union
select 2 , 'xx6' , 'dd' union
select 2 , 'xx8' , 'dd'union
select 2 , 'xx9', 'dd' union
select 2 , 'xx10' , 'dd' union
select 2 , 'xx11' , 'dd' union
select 2 , 'xx17' , 'dd' union
select 2 , 'xx27' , 'dd'
-----------------------------------------------
select * ,(select sum(1)from a_test (nolock) where CategoriesID=a.CategoriesID and [id]<=a.[id] ) as Tid
into #tmp
from a_test a(nolock)
select [id],categoriesid,title,content ,tid
from #tmp where tid<=10
order by categoriesid,tid
--drop table #tmp delete from atest
---------test ------------
id categoriesid title content tid
----------- ------------ -------------------- ---------- -----------
1 1 xx1 dd 1
2 1 xx2 dd 2
3 1 xx3 dd 3
8 1 xx1 dd 4
9 1 xx2 dd 5
10 1 xx3 dd 6
20 1 xx1 dd 7
21 1 xx2 dd 8
22 1 xx3 dd 9
32 1 xx1 dd 10
4 2 xx4 dd 1
5 2 xx5 dd 2
6 2 xx6 dd 3
7 2 xx7 dd 4
11 2 xx10 dd 5
12 2 xx11 dd 6
13 2 xx17 dd 7
14 2 xx27 dd 8
15 2 xx4 dd 9
16 2 xx5 dd 10
(影響 20 個資料列)
#2
Select * from Table1 a where ID in
(select Top 10 ID From Table1 b where b.CategoriesID = a.CategoriesID)
(select Top 10 ID From Table1 b where b.CategoriesID = a.CategoriesID)
#3
select * from 表 a
where id in(
select top 10 id from 表
where CategoriesID=a.CategoriesID)
where id in(
select top 10 id from 表
where CategoriesID=a.CategoriesID)
#4
这条语句怎么解释
#1
create table A_test( [id] int IDENTITY(1,1) ,CategoriesID int, Title varchar(20),content varchar(10))
---------------------------------
insert into A_test
select
1 , 'xx1', 'dd' union
select 1 , 'xx2', 'dd' union
select 1 , 'xx3' , 'dd' union
select 2 , 'xx4', 'dd' union
select 2 , 'xx5' , 'dd' union
select 2 , 'xx6' , 'dd' union
select 2 , 'xx8' , 'dd'union
select 2 , 'xx9', 'dd' union
select 2 , 'xx10' , 'dd' union
select 2 , 'xx11' , 'dd' union
select 2 , 'xx17' , 'dd' union
select 2 , 'xx27' , 'dd'
----------------------------------------------------
insert into A_test
select
1 , 'xx1', 'dd' union
select 1 , 'xx2', 'dd' union
select 1 , 'xx3' , 'dd' union
select 2 , 'xx4', 'dd' union
select 2 , 'xx5' , 'dd' union
select 2 , 'xx6' , 'dd' union
select 2 , 'xx8' , 'dd'union
select 2 , 'xx9', 'dd' union
select 2 , 'xx10' , 'dd' union
select 2 , 'xx11' , 'dd' union
select 2 , 'xx17' , 'dd' union
select 2 , 'xx27' , 'dd'
-----------------------------------------------
select * ,(select sum(1)from a_test (nolock) where CategoriesID=a.CategoriesID and [id]<=a.[id] ) as Tid
into #tmp
from a_test a(nolock)
select [id],categoriesid,title,content ,tid
from #tmp where tid<=10
order by categoriesid,tid
--drop table #tmp delete from atest
---------test ------------
id categoriesid title content tid
----------- ------------ -------------------- ---------- -----------
1 1 xx1 dd 1
2 1 xx2 dd 2
3 1 xx3 dd 3
8 1 xx1 dd 4
9 1 xx2 dd 5
10 1 xx3 dd 6
20 1 xx1 dd 7
21 1 xx2 dd 8
22 1 xx3 dd 9
32 1 xx1 dd 10
4 2 xx4 dd 1
5 2 xx5 dd 2
6 2 xx6 dd 3
7 2 xx7 dd 4
11 2 xx10 dd 5
12 2 xx11 dd 6
13 2 xx17 dd 7
14 2 xx27 dd 8
15 2 xx4 dd 9
16 2 xx5 dd 10
(影響 20 個資料列)
---------------------------------
insert into A_test
select
1 , 'xx1', 'dd' union
select 1 , 'xx2', 'dd' union
select 1 , 'xx3' , 'dd' union
select 2 , 'xx4', 'dd' union
select 2 , 'xx5' , 'dd' union
select 2 , 'xx6' , 'dd' union
select 2 , 'xx8' , 'dd'union
select 2 , 'xx9', 'dd' union
select 2 , 'xx10' , 'dd' union
select 2 , 'xx11' , 'dd' union
select 2 , 'xx17' , 'dd' union
select 2 , 'xx27' , 'dd'
----------------------------------------------------
insert into A_test
select
1 , 'xx1', 'dd' union
select 1 , 'xx2', 'dd' union
select 1 , 'xx3' , 'dd' union
select 2 , 'xx4', 'dd' union
select 2 , 'xx5' , 'dd' union
select 2 , 'xx6' , 'dd' union
select 2 , 'xx8' , 'dd'union
select 2 , 'xx9', 'dd' union
select 2 , 'xx10' , 'dd' union
select 2 , 'xx11' , 'dd' union
select 2 , 'xx17' , 'dd' union
select 2 , 'xx27' , 'dd'
-----------------------------------------------
select * ,(select sum(1)from a_test (nolock) where CategoriesID=a.CategoriesID and [id]<=a.[id] ) as Tid
into #tmp
from a_test a(nolock)
select [id],categoriesid,title,content ,tid
from #tmp where tid<=10
order by categoriesid,tid
--drop table #tmp delete from atest
---------test ------------
id categoriesid title content tid
----------- ------------ -------------------- ---------- -----------
1 1 xx1 dd 1
2 1 xx2 dd 2
3 1 xx3 dd 3
8 1 xx1 dd 4
9 1 xx2 dd 5
10 1 xx3 dd 6
20 1 xx1 dd 7
21 1 xx2 dd 8
22 1 xx3 dd 9
32 1 xx1 dd 10
4 2 xx4 dd 1
5 2 xx5 dd 2
6 2 xx6 dd 3
7 2 xx7 dd 4
11 2 xx10 dd 5
12 2 xx11 dd 6
13 2 xx17 dd 7
14 2 xx27 dd 8
15 2 xx4 dd 9
16 2 xx5 dd 10
(影響 20 個資料列)
#2
Select * from Table1 a where ID in
(select Top 10 ID From Table1 b where b.CategoriesID = a.CategoriesID)
(select Top 10 ID From Table1 b where b.CategoriesID = a.CategoriesID)
#3
select * from 表 a
where id in(
select top 10 id from 表
where CategoriesID=a.CategoriesID)
where id in(
select top 10 id from 表
where CategoriesID=a.CategoriesID)
#4
这条语句怎么解释