-1

我设法拼凑了一个连接语句,为我提供了一个显示整个数据库的表。现在我需要将结果缩小到最近 30 天,但我一生都无法弄清楚如何将此限制应用于查询。

现在我有:

select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate 
from customers 
where orderinfo.orderdate = curdate() 
inner join orderinfo on customers.id=orderinfo.fk_id 
inner join status on orderinfo.fk_code=status.code 
order by customers.name;

...试图缩小范围以向我展示今天的所有记录,但即使这样也行不通。我看到有多种方法可以做 30 天的事情,但我遇到最多的问题就是让 WHERE 子句在这个查询中完全起作用。

感谢您的任何建议!

4

4 回答 4

1

WHERE子句需要ORDER BY在所有连接之前和之后。

select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate 
from customers 
inner join orderinfo on customers.id=orderinfo.fk_id 
inner join status on orderinfo.fk_code=status.code 
where orderinfo.orderdate = curdate() 
order by customers.name;
于 2012-12-01T09:02:51.283 回答
0

你没有orderinfo在你的FROM部分指定。

select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate 
from customers 
inner join orderinfo on customers.id=orderinfo.fk_id 
inner join status on orderinfo.fk_code=status.code 
where orderinfo.orderdate = curdate() 
order by customers.name;
于 2012-12-01T09:02:37.937 回答
0
SELECT customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate 
FROM customers 
INNER JOIN orderinfo ON customers.id=orderinfo.fk_id 
INNER JOIN status ON orderinfo.fk_code=status.code 
WHERE orderinfo.orderdate = curdate() 
ORDER BY customers.name;

WHERE毕竟JOINS和之前需要ORDER BY

于 2012-12-01T09:04:31.130 回答
0

你的内部连接应该在你的 where 子句之前

select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate 
from customers 
inner join orderinfo on customers.id=orderinfo.fk_id 
inner join status on orderinfo.fk_code=status.code 
where orderinfo.orderdate = curdate() 
order by customers.name;

如果您需要先使用条件进行查询,那么您需要子查询

Select * From
(
   select customers.name, customers.email, orderinfo.orderno, orderinfo.orderdate, status.state, orderinfo.lastupdate 
   from customers
   where orderinfo.orderdate = curdate()  
) customers
inner join orderinfo on customers.id=orderinfo.fk_id 
inner join status on orderinfo.fk_code=status.code 
order by customers.name;
于 2012-12-01T09:04:47.063 回答