select o.customerid, c.customername, o.orderdate
from orders as o, customers as c
where o.orderdate='1997-08-26';
使用示例 northwind db,我无法弄清楚出了什么问题?我使用了示例表中使用的日期格式。
我正在尝试提取 26 日下订单的任何人的 ID 和姓名。
select o.customerid, c.customername, o.orderdate
from orders as o, customers as c
where o.orderdate='1997-08-26';
使用示例 northwind db,我无法弄清楚出了什么问题?我使用了示例表中使用的日期格式。
我正在尝试提取 26 日下订单的任何人的 ID 和姓名。
您需要JOIN
将orders
和customers
表相互连接:
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
语法的内容。
此方法将允许查询在 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'
尝试这个
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 ';
更新为加入