SQL:列出星期一订购的产品

时间:2016-11-22 03:55:38

标签: sql sql-server

我正在尝试查询星期一订购的产品,而我对如何格式化或提取特定的星期几为orderDate值感到困惑...

这就是我所拥有的......(我对SQL很新,所以我可能会离开......)

SELECT Products.productName, OrderDetails.orderNumber, Orders.orderDate

FROM Orders, OrderDetails, Products

WHERE Orders.orderDate LIKE '%Monday%; 

提前致谢!

4 个答案:

答案 0 :(得分:2)

SELECT  Products.productName , 
        OrderDetails.orderNumber , 
        DATENAME(dw,Orders.orderDate),  
        Orders.orderDate  
FROM Orders 
inner join OrderDetails  
   on OrderDetails.OrderID = Orders.ID 
inner join Products 
   on Products.ID = Orders.ProductID  
WHERE DATENAME(dw,Orders.orderDate) = 'Monday'

答案 1 :(得分:1)

SELECT 
   Products.
   productName, 
   OrderDetails.orderNumber, 
   Orders.orderDate
FROM 
Orders 
JOIN OrderDetails ON Orders.OrdersID = OrderDetails.OrderID
JOIN Products ON OrderDetail.ProductID = Products.ProductID
WHERE 
DATENAME(DW,Orders.orderDate) = 'Monday'

答案 2 :(得分:1)

使用Datepart功能,DATEPART(W,[DATE])将返回工作日。 (星期一为1)

SELECT Products.productName, OrderDetails.orderNumber, Orders.orderDate

FROM Orders, OrderDetails, Products

WHERE DATEPART(w,Convert(Date,Orders.orderDate))=1

注意:这将产生笛卡尔积的结果。请正确加入这些表格

答案 3 :(得分:0)

您可以使用sql date函数:
Oracle: to_char(Orders.orderDate,'D')
MySql: DAYOFWEEK(Orders.orderDate)