最近订单的SQL查询

时间:2016-08-30 05:27:14

标签: mysql sql sql-server oracle

我有以下两个表:

用户[表]

UserID [PK] (Integer)
FirstName (VarChar)
LastName (VarChar)
EmailAddress (VarChar)
RegistrationDate (DateTime)

订单[表格]

OrderId [PK] (Integer)
UserId [FK] (Integer)
OrderDate (DateTime)
OrderFulfilled (Boolean)

我需要编写一个查询来显示当天之前提交的尚未完成订单的最近100个订单。我需要包括他们的订单日期,订单ID,电子邮件地址和注册日期。

有人可以帮我这个吗?

由于

2 个答案:

答案 0 :(得分:0)

在MS SQL Server环境中使用SELECT TOP语句:

SELECT TOP 100 *
FROM Orders o
INNER JOIN Users u
ON o.UserID = u.UserID
WHERE OrderDate < CAST(SYSDATETIME() AS DATE)
AND OrderFulfilled = 0
ORDER BY OrderDate DESC

对于oracle使用:

ROWNUM <= 100

对于mySql使用:

LIMIT 100

答案 1 :(得分:0)

此查询适合您 -

SELECT OrderId, OrderDate, EmailAddress, RegistrationDate
FROM Orders as o
INNER JOIN Users as u
ON o.UserID = u.UserID
WHERE OrderDate < CURDATE()
AND OrderFulfilled = 0
ORDER BY OrderDate DESC
LIMIT 0,100