【问题标题】:MySQL - What's wrong with the WHERE clause in my JOIN query?MySQL - 我的 JOIN 查询中的 WHERE 子句有什么问题?
【发布时间】:2012-12-01 09:00:38
【问题描述】:

我设法拼凑了一个连接语句,为我提供了一个显示整个数据库的表。现在我需要将结果缩小到最近 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 子句在这个查询中完全起作用。

感谢您的建议!

【问题讨论】:

    标签: mysql date where


    【解决方案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;
    

    【讨论】:

    • 我知道这很简单。谢谢!
    • 我很高兴 ivan - 很高兴能帮上忙。
    【解决方案2】:

    您没有在 FROM 部分中指定 orderinfo

    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;
    

    【讨论】:

      【解决方案3】:

      你的内部连接应该在你的 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;
      

      【讨论】:

        【解决方案4】:
        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 之前

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-18
          • 1970-01-01
          • 1970-01-01
          • 2019-11-10
          • 1970-01-01
          • 2020-05-05
          • 2017-08-24
          • 1970-01-01
          相关资源
          最近更新 更多