如何在mysql查询中跳过行

时间:2013-06-25 07:57:52

标签: mysql sql stored-procedures

这是我的sp代码。我想从这个sp的结果中选择下一个不同的6行。我怎么能这样做?

SELECT  N.NewsId,
        N.HeadCaption,
        (SELECT Name FROM NewsCategory 
         WHERE NewsCategoryId = N.HeadLineCategoryId) Category,
        N.PicUrl,
        N.Creation,
        SUBSTRING((fnStripTags(N.Description)),1,75) AS ShortDescription
FROM    News N
INNER JOIN
        (SELECT  HeadlineCategoryID, MAX(NewsID) max_id
         FROM    News           
         GROUP   BY HeadlineCategoryID) N_ 
   ON N.HeadlineCategoryID = N_.HeadlineCategoryID AND
      N.NewsID = N_.max_id
ORDER BY N.ViewIndex DESC  
LIMIT 6;  

3 个答案:

答案 0 :(得分:2)

好的,好的,就像这样

SELECT  N.NewsId,
        N.HeadCaption,
        (SELECT Name FROM NewsCategory 
         WHERE NewsCategoryId = N.HeadLineCategoryId) Category,
        N.PicUrl,
        N.Creation,
        SUBSTRING((fnStripTags(N.Description)),1,75) AS ShortDescription
FROM    News N
INNER JOIN
        (SELECT  HeadlineCategoryID, MAX(NewsID) max_id
         FROM    News           
         GROUP   BY HeadlineCategoryID) N_ 
   ON N.HeadlineCategoryID = N_.HeadlineCategoryID AND
      N.NewsID = N_.max_id
ORDER BY N.ViewIndex DESC  
LIMIT 6 limit 6; 

在代码末尾,使用“limit 6”,您可以从表/源

获得接下来的6行

答案 1 :(得分:0)

尝试使用SELECT DISTINCT而不是SELECT

答案 2 :(得分:0)

使用LIMIT功能

  

LIMIT子句可用于约束返回的行数   通过SELECT语句。 LIMIT需要一个或两个数字参数,   它必须都是非负整数常量,有两个参数,   第一个参数指定要返回的第一行的偏移量,   第二个指定要返回的最大行数。