【问题标题】:ignoring saturday and sunday忽略周六和周日
【发布时间】:2010-03-20 23:04:23
【问题描述】:

我正在从我的客户数据库(mysql)中提取过去十天的所有记录
$offset1 =strtotime("-10 天"); $date3=date("Y-m-d",$offset1);

SELECT * FROM customers WHERE date between '$date3' and '$date' AND customer.custid = '$custid' ORDER by date DESC

我想省略周六或周日的日期,并希望将其放在我的查询中而不是 php 中

如果你能帮忙谢谢

【问题讨论】:

    标签: date mysql


    【解决方案1】:

    您可以使用 MySQL 中的 DayOfWeek 函数。

    SELECT * 
    FROM customers 
    WHERE date between '$date3' and '$date'
          AND DayOfWeek(date) <> 1
          AND DayOfWeek(date) <> 7
          AND customer.custid = '$custid' 
    ORDER by date DESC
    

    【讨论】:

    • 您可能会尝试如何表达绑定检查,看看是否可以找到删除这对检查的方法:DayOfWeek(date) % 7 &gt; 1。与往常一样,benchmark 因为它更难阅读,引擎可能会为您执行此操作。
    【解决方案2】:

    我认为你可以使用 DAYNAME 函数:

    AND DAYNAME(date) NOT IN ('Saturday', 'Sunday')
    

    DAYNAME(date)

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      相关资源
      最近更新 更多