你如何在SQL中选择一个确切的日期?

时间:2013-07-03 19:45:05

标签: sql sql-server date

select o.customerid, c.customername, o.orderdate
from orders as o, customers as c
where o.orderdate='1997-08-26';

使用示例northwind db,我无法弄清楚出了什么问题?我使用了样本表中使用的日期格式。

我正在尝试提取26日下订单的任何人的身份证和姓名。

3 个答案:

答案 0 :(得分:6)

您需要JOIN orderscustomers表格彼此:

select o.customerid, c.customername, o.orderdate
from orders as o, customers as c
where o.orderdate='19970826'
AND o.customerid = c.customerid

使用显式语法:

SELECT o.customerid, 
       c.customername, 
       o.orderdate 
FROM   orders AS o 
       JOIN customers c 
         ON c.customerid = o.customerid 
WHERE  o.orderdate = '19970826' 

您还应该阅读explicit vs. implicit JOIN语法。

答案 1 :(得分:1)

此方法将允许查询在orderdate上使用索引:

SELECT o.customerid, 
       c.customername, 
       o.orderdate 
FROM   orders AS o 
       JOIN customers c 
         ON c.customerid = o.customerid 
WHERE  o.orderdate >= '1997-08-26' 
AND o.orderdate < '1997-08-27' 

答案 2 :(得分:-3)

试试这个

  

选择o.customerid,c.customername,o.orderdate   从订单作为内部加入客户作为c   on o.customerid = c.customerid   其中CONVERT(VARCHAR(10),o.orderdate,120)='1997-08-26';

更新了加入