我有一个相当漫长而复杂的SQL查询,我正在研究这个问题.
我想要做的只是添加LIMIT 10...但每次我结束它到查询的结尾我都会收到错误.
任何帮助都会很棒,查询如下:
sqlQuery = "
select DATENAME(Month,i.Datecreated) + ' ' +
DATENAME(day,i.Datecreated) + ' ' +
DATENAME(year,i.Datecreated) AS USDateCreated,
i.imageId,
GalleryName,Fullpath,MediumPath,ThumbPath,ViewCounter,
i.DateCreated,ItemNumber,Gender,Minutes,
right(convert(varchar(3), 100 + Seconds),2) as Seconds,
FramesPerSecond,WeekNumber,Filename,
(round(cast(Size as Decimal(16,2))/1024,2)) as Size,
FlvFilename,FlvSize,NumberOfMovies,
Free,Comment,
(case when sum(rating)/count(i.imageId) is null then 0 else sum(rating)/count(i.imageId) end) as ratingResult,
dbo.getTagNames(i.imageid) as tagsname,'' as yourTagNames,
dbo.getTagNames(i.imageid) as memberTagNames,max(weekNumber)-1 as lastWeek
from images as i
left join Imagerating as ir on i.imageId = ir.imageId
left join tag as t on i.imageId = t.imageId where 1=1
and galleryName = 'pictures'
and weekNumber = '4'
group by i.imageId,GalleryName,Fullpath,MediumPath,ThumbPath,
ViewCounter,i.DateCreated,ItemNumber,Gender,Minutes,Seconds,
FramesPerSecond,WeekNumber,Filename,Size,FlvFilename,FlvSize,
NumberOfMovies,Free,Comment
order by filename
"
Run Code Online (Sandbox Code Playgroud)
T-SQL不支持LIMIT.相反,请TOP在您的SELECT:
SELECT TOP 100 -- Rather than LIMIT 100 at the bottom
Field1, Field2 -- etc.
FROM YourTable
GROUP BY Field1
ORDER BY Field2
Run Code Online (Sandbox Code Playgroud)
如果您使用的是SQL Server 2012或更高版本,则可以使用OFFSET和FETCH [FIRST|NEXT]获取LIMIT分页结果集的功能.