如何获取范围内的数据,如"限制"在Mysql中使用Mssql中的top

时间:2014-10-06 06:35:59

标签: php mysql sql sql-server

我有这个查询将从多个表中获取数据,问题是它是由我的客户端提供的,我无法改变太多。我需要的是,我想在此查询中设置限制范围(限制0,5),但似乎限制在Msssql中不起作用。那么有什么办法可以在此范围内设置限制范围来获取数据。

 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 个答案:

答案 0 :(得分:2)

尝试使用Top或RowNumber,例如此示例

看看这个How to implement LIMIT with Microsoft SQL Server?

答案 1 :(得分:0)

试试这样: 对于SQL SERVER 2008

select * from 
(
select ROW_NUMBER()  OVER(ORDER BY D.Item_Number) as ROWNUMBER,D.* from 
(
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%'

)D

)M
where M.ROWNUMBER between 1 and 5

答案 2 :(得分:0)

对于2008年,您可以做很多事情。

如果您始终只想要X并且偏移量为0,那么可以使用许多可以轻松实现的切割。 您可以在UNION:

之前使用SET ROWCOUNT
SET ROWCOUNT 5
<your union>

或者 - 您可以通过包装公用表表达式在整个联合中使用TOP:

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

如果你打算用它进行分页,这意味着你想要一个大于0的偏移,那么你应该开始研究ROW_Number(),其他答案也会显示出来。

;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

(存在许多版本的语法,这只是一个显示)。

您也可以将TOP与NOT IN结合使用,但ROW_Number()更容易,也是一个更通用的工具。