如何使用以下Microsoft服务器查询实现LIMIT

时间:2016-08-08 17:42:32

标签: sql sql-server

我有很长的SQL查询,大约需要35秒来执行数据。所以我想将数据限制为10个记录,每个记录都有偏移量,并且像mysql一样限制到下面的mssql查询?

SELECT     distinct PAsGroupBuyData.groupPA, PAsGroupBuyData.dateShipOn, PAsGroupBuyData.dateShipOn2, PAsGroupBuyData.packingSlipsMade, PAsGroupBuyData.productionTime, PAsGroupBuyData.reorder, DSD.designName, DSD.designCode, DSD.designId, 
                  DSD.dsId, PAsGroupBuyData.machineId, orderType = 1
                        FROM         Quotes_Manufacturer INNER JOIN
                                              PAsData ON PAsData.PA = Quotes_Manufacturer.PA INNER JOIN
                                              PAsGroupBuyData ON PAsGroupBuyData.groupPA = PAsData.GroupPa INNER JOIN
                                              DesignSystemDesigns DSD ON PAsGroupBuyData.designId = DSD.designId
                        WHERE    (PAsGroupBuyData.status = 3) AND (Quotes_Manufacturer.accepted = 1) and (PAsGroupBuyData.dateShipOn <> '".$thisSunday."') and (PAsGroupBuyData.dateShipOn <> '".$lastSunday."')
                        UNION ALL
                        SELECT    distinct PAsGroupBuyData.groupPA, PAsGroupBuyData.dateShipOn, PAsGroupBuyData.dateShipOn2, PAsGroupBuyData.packingSlipsMade, PAsGroupBuyData.productionTime, PAsGroupBuyData.reorder, DSD.designName, DSD.designCode, DSD.designId, 
                                              DSD.dsId, PAsGroupBuyData.machineId, orderType = 2
                        FROM         Quotes_SupplierAndManufacturerGroupBuy INNER JOIN
                                              PAsGroupBuyData ON PAsGroupBuyData.groupPA = Quotes_SupplierAndManufacturerGroupBuy.groupPA INNER JOIN
                                              DesignSystemDesigns DSD ON PAsGroupBuyData.designId = DSD.designId INNER JOIN
                                              PAsData ON PAsData.GroupPa = PAsGroupBuyData.groupPA
                        WHERE     (PAsGroupBuyData.status = 3) AND (Quotes_SupplierAndManufacturerGroupBuy.accepted = 1) and (PAsGroupBuyData.dateShipOn <> '".$thisSunday."') and (PAsGroupBuyData.dateShipOn <> '".$lastSunday."')
                        UNION ALL
                        SELECT     PAsData.PA, PAsData.dateShipOn, PAsData.dateShipOn2, PAsData.packingSlipsMade, PAsData.productionTime, PAsData.reorder, DSD.designName, DSD.designCode, DSD.designId, DSD.dsId, 
                                              PAsData.machineId, orderType = 3
                        FROM         Quotes_Manufacturer INNER JOIN
                                              PAsData ON PAsData.PA = Quotes_Manufacturer.PA INNER JOIN
                                              DesignSystemDesigns DSD ON PAsData.designId = DSD.designId
                        WHERE      (Quotes_Manufacturer.accepted = 1) AND  (PAsData.status = 3) AND (PAsData.GroupPa = 0) and (PAsData.dateShipOn <> '".$thisSunday."') and (PAsData.dateShipOn <> '".$lastSunday."')
                        UNION ALL
                        SELECT     PAsData.PA, PAsData.dateShipOn, PAsData.dateShipOn2, PAsData.packingSlipsMade, PAsData.productionTime, PAsData.reorder, DSD.designName, DSD.designCode, DSD.designId, DSD.dsId, 
                                              PAsData.machineId, orderType = 4
                        FROM         Quotes_SupplierAndManufacturer INNER JOIN
                                              PAsData ON PAsData.PA = Quotes_SupplierAndManufacturer.PA INNER JOIN
                                              DesignSystemDesigns DSD ON PAsData.designId = DSD.designId
                        WHERE      (PAsData.status = 3) AND (Quotes_SupplierAndManufacturer.accepted = 1) AND (Pasdata.groupPa = 0)  and (PAsData.dateShipOn <> '".$thisSunday."') and (PAsData.dateShipOn <> '".$lastSunday."')
                        UNION ALL
                        SELECT     ID As PA, _Date As dateShipOn, _Date As dateShipOn2, 0 As packingSlipsMade, productionTime, 0 As reorder, Title As designName, ' ' As designCode, 0 As designId, 0 As dsId, 
                                              machineId, orderType = 5
                        FROM         Calendar
                        WHERE     (_Date <> '".$thisSunday."') and (_Date <> '".$lastSunday."') and (_Date > getDate()-1) and (_Date < (getDate()+30)) 
                        ORDER BY PAsGroupBuyData.dateShipOn, PAsGroupBuyData.machineId, PAsGroupBuyData.groupPA

1 个答案:

答案 0 :(得分:0)

感谢戈登。我找到了使用偏移量和mssql查询限制的正确语法。

 Example: offset ".$offset." ROWS FETCH NEXT ".$limit." ROWS ONLY