我有两个表 - 项目和订单。
Item
=====================
ID, ItemName
Order
========================
orderID, ID , status
我想使用内部联接加入表:
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
提前致谢。
答案 0 :(得分: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
答案 1 :(得分: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