0

我有两个表 - 项目和订单。

Item  
=====================
ID, ItemName 


Order 
========================
orderID, ID , status

我想通过使用 inner join 加入表:

SELECT Order.orderID, Order.ID, Item.ItemName
FROM Order
INNER JOIN Item
ON Order.ID=Item.ID

但是我需要在php上进行分页的问题。

我有代码,但我不知道如何修改它,因为我的 sql 不是很好。

这是代码(我已经对其进行了修改并能够使用“*”显示,但不知道如何在下面的代码中应用内连接):

SELECT TOP 20 * FROM 
(
SELECT  ROW_NUMBER() OVER (ORDER BY Order.orderID) AS RowNumber, *, TotalRows=Count(*) OVER()
FROM Order WHERE status=1
) _tmpInlineView
WHERE RowNumber >= 20 AND status=1 ORDER BY Order.orderID

提前致谢。

4

2 回答 2

1

将您的加入放在订单表下方:

SELECT TOP 20 * FROM 
(
SELECT  ROW_NUMBER() OVER (ORDER BY Order.orderID) AS RowNumber, Order.orderID, Order.ID, Item.ItemName, TotalRows=Count(*) OVER()
FROM Order INNER JOIN Item
ON Order.ID=Item.ID WHERE status=1
) _tmpInlineView
WHERE RowNumber >= 20 ORDER BY _tmpInlineView.orderID
于 2013-04-01T07:04:21.323 回答
1

试试这个

      SELECT TOP 20 * FROM 
        (
        SELECT  ROW_NUMBER() OVER (ORDER BY Order.orderID) AS RowNumber, ORDER.*, 
Count(1) OVER() as TotalRows
        FROM ORDER INNER JOIN Item ON Order.ID=Item.ID
         WHERE status=1
        ) _tmpInlineView
        WHERE RowNumber >= 20 ORDER BY orderID

或者试试这个

    SELECT * 
    FROM ORDER INNER JOIN Item ON Order.ID=Item.ID WHERE status=1
    ORDER BY Order.orderID 
    OFFSET ( @PageNo - 1 ) * @RecordsPerPage ROWS
    FETCH NEXT @RecordsPerPage ROWS ONLY

The following are the limitations of using Offset Fetch:
1) Fetch Next can't be used standalone, it requires Offset
2) Offset can't be used standalone, it requires order by
3) Top can't be combined with offset fetch next in the same query expression
于 2013-04-01T07:07:03.250 回答