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

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

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

4

3 回答 3

6

您需要JOINorderscustomers表相互连接:

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' 

您还应该阅读有关显式与隐式 JOIN语法的内容。

于 2013-07-03T19:46:01.993 回答
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' 
于 2013-07-03T19:54:34.930 回答
-3

尝试这个

select o.customerid, c.customername, o.orderdate from orders as o inner join customers as c on o.customerid=c.customerid where CONVERT(VARCHAR(10), o.orderdate, 120)='1997-08-26 ';

更新为加入

于 2013-07-03T19:49:00.367 回答