donglun2024 2014-10-06 06:35
浏览 128
已采纳

如何使用Mssql中的top在Mysql中获取范围内的数据,如“limit”

I have this query which will fetch data from multiple tables and the problem is that it was given by my client which i could not alter too much . What i need is that I want to put limit range like (limit 0,5) in this query but it seems limit is not working in Msssql. So is there any way I could put limit range in this to fetch data .

 select  distinct 
            item.[No_] as Item_Number
            ,item.[Description 3] as Title
            ,item.[ISBN]
            ,item.[Edition] as Edition
            ,item.[Copyright Year] as Copyright_year
            ,item.[Unit Price] as Price
            ,item.[Description] as Author_short
            ,ID.[Notes] as Page_count
            ,M.[Description] as Media_code
            ,item.[Height] as Height
            ,item.[Length] as "Length"
            ,item.[Width] as Width
            ,item.[Net Weight] as "Weight"
            ,item.[Tangible] as Is_Tangible
        from
            "Westmark-Production".dbo.GRT_Prod$Item as item
            join "Westmark-Production".dbo.GRT_Prod$Job as job on item.[No_] = job.[Job Project No_]
            left outer join "Westmark-Production".dbo."GRT_Prod$Item Interior Details" ID ON (ID.[Item No_] = item.[No_] and ID.[Interior Code] = 'FINISHED PAGE COUNT')
            left outer join "Westmark-Production".dbo."GRT_Prod$Item Media Code" M ON M.[Item No_] = item.[No_]
            left outer join "Westmark-Production".dbo."GRT_Prod$Item Ledger Entry" ILE ON ILE.[Item No_] = item.[No_] AND ILE.[Location Code] = 'D01'
        where 1=1
            and item.[Item Disc_ Group] <> '19'
            and item.[Global Dimension 1 Code] <> 'OTHER'
            and len(item.[ISBN]) > 1
            and len(item.[Booklink]) > 1
            and len(item.[No_]) = 6
            and ((job.[Status] <> '3' and item.[Blocked] <> 1) or item.[BlockedReason] NOT IN (1,2,4,5))
            and item.[Tangible] = 1 
            and item.[Product Group Code] not in ('CP','CP KP','KP CP')
            and item.[Prep Type] <> 3
            and item.[Description 3] NOT LIKE '%- ECOMMERCE%'

        union
        select distinct 
            item.[No_] as Item_Number
            ,item.[Description 3] as Title
            ,item.[ISBN]
            ,item.[Edition] as Edition
            ,item.[Copyright Year] as Copyright_year
            ,item.[Unit Price] as Price
            ,item.[Description] as Author_short
            ,ID.[Notes] as Page_count
            ,M.[Description] as Media_code
            ,item.[Height] as Height
            ,item.[Length] as "Length"
            ,item.[Width] as Width
            ,item.[Net Weight] as "Weight"
            ,item.[Tangible] as Is_Tangible
        from
            "Westmark-Production".dbo.GRT_Prod$Item as item
            left outer join "Westmark-Production".dbo."GRT_Prod$Item Interior Details" ID ON (ID.[Item No_] = item.[No_] and ID.[Interior Code] = 'FINISHED PAGE COUNT')
            left outer join "Westmark-Production".dbo."GRT_Prod$Item Media Code" M ON M.[Item No_] = item.[No_]
            left outer join "Westmark-Production".dbo."GRT_Prod$Item Ledger Entry" ILE ON ILE.[Item No_] = item.[No_] AND ILE.[Location Code] = 'D01'
        where 1=1
            and len(item.[Booklink]) > 1
            and len(item.[No_]) = 6
            and item.[Description 3] NOT LIKE '%- ECOMMERCE%'
  • 写回答

3条回答 默认 最新

  • duanfoumi5620 2014-10-06 08:13
    关注

    For 2008 there are a number of things you can do.

    If you always want just the X first and have the offset of 0, then there are a number of shotcuts that can be taken to make it easily. You can either use SET ROWCOUNT before your UNION:

    SET ROWCOUNT 5
    <your union>
    

    Alternatively - you can use TOP over the entirety of your union by wrapping in a Common Table Expression:

    ;WITH CTE AS (
    <your union>
    )
    SELECT TOP 5 
    FROM CTE.
    

    Alternative if you are going to use it for paging, meaning you want an offset larger than 0, then you should start looking into ROW_Number() as the other answers also show.

    ;WITH CTE AS (
    <your union>
    ), CTE2 AS (
      SELECT *, ROW_Number() OVER (ORDER BY Item_Number) AS RN
    )
    SELECT * FROM CTE2 
    WHERE RN BETWEEN 0 AND 5
    

    (many versions of the syntax exists, this is just one to show).

    You could also use the TOP combined with a NOT IN, but ROW_Number() is much easier and a much more versatile tool.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。