我想在网格视图上显示特定客户的最后一个订单,我所做的是显示客户的所有订单,但我需要最后一个订单
这是我的SQL代码
SELECT orders.order_id, orders.order_date,
orders.payment_type, orders.cardnumber, packages.Package_name,
orders.package_id, packages.package_price
FROM orders INNER JOIN packages ON orders.package_id = packages.Package_ID
WHERE (orders.username = @username )
@username从cookie中获取其值,现在我如何才能选择最后一个订单仅用于cookie值“Tony”?
答案 0 :(得分:3)
为了概括(并修复一下)Mitch的答案,你需要使用带有TOP(@N)和ORDER BY ... DESC的SELECT子句。请注意,我使用TOP(@N),而不是TOP N,这意味着您可以将它作为参数传递给存储过程并返回,例如,不是1但是N最后一个订单:
CREATE STORED PROCEDURE ...
@N int
...
SELECT TOP(@N) ...
ORDER BY ... DESC
答案 1 :(得分:1)
SELECT top 1
orders.order_id,
orders.order_date,
orders.payment_type,
orders.cardnumber,
packages.Package_name,
orders.package_id,
packages.package_price
FROM orders
INNER JOIN packages ON orders.package_id = packages.Package_ID
WHERE (orders.username = @username )
ORDER BY orders.order_date DESC
实际上假设orders.order_id
是一个标识列:
SELECT top 1
orders.order_id,
orders.order_date,
orders.payment_type,
orders.cardnumber,
packages.Package_name,
orders.package_id,
packages.package_price
FROM orders
INNER JOIN packages ON orders.package_id = packages.Package_ID
WHERE (orders.username = @username )
ORDER BY orders.order_id DESC