【问题标题】:How to use like and between together in sql?如何在sql中同时使用like和between?
【发布时间】:2018-11-01 02:18:38
【问题描述】:
$query = "SELECT * FROM justshawarma_nexo_commandes where REF_CLIENT = '$uuc' 
and DATE_CREATION like '%$date3%' and DATE_CREATION like '%$date4%'";

这是我的查询。我想获取两个日期之间匹配的记录。但是我在表格中的日期列同时包含日期和时间。这就是为什么我要使用 like 和 between。

这段代码有什么错误?

【问题讨论】:

  • 使用之间? ...在“$date3”和“$date4”之间的 DATE_CREATION

标签: php mysql database web


【解决方案1】:
   $between['date_from'] =  date("Y-m-d", strtotime($date3))." 00:00:00";
    $between['date_to'] = date("Y-m-d",strtotime($date4))." 23:59:59";


 $query = "SELECT * FROM justshawarma_nexo_commandes where REF_CLIENT = '$uuc' and DATE_CREATION BETWEEN '".$between['date_from']."' AND '".$between['date_to']."';

【讨论】:

    【解决方案2】:

    使用 BETWEEN 条件:

    $query = "SELECT * FROM justshawarma_nexo_commandes where REF_CLIENT = '$uuc' and DATE_CREATION BETWEEN '$date3' and '$date4'";
    

    【讨论】:

    • 我在表格中的日期列包含日期和时间。
    【解决方案3】:

    使用 Like 您的查询将如下所示

    $query = "SELECT * FROM justshawarma_nexo_commandes 
              where REF_CLIENT = \'$uuc\' 
              and DATE_CREATION like %\'$date3\'% 
              and DATE_CREATION like %\'$date4\'%";
    

    在之间

    $query = "SELECT * FROM justshawarma_nexo_commandes 
              where REF_CLIENT = \'$uuc\' 
              and DATE_CREATION Between \'$date3 00:00:00\' 
              and \'$date4 23:59:59\'";
    

    正如您所说,您的专栏中有时间,因此您可以使用这些查询选择该日期范围内的所有记录。

    【讨论】:

    • 当您复制和粘贴这些查询时,一些转义 `` 添加的位置。我敢肯定你不是那个意思..
    【解决方案4】:

    试试这个。这对我来说很好用,这是我的 Order_ID 的一部分有日期:

      SELECT *
              FROM [MABH-Desi-Dera].[dbo].[Order] where(CONVERT(nvarchar,[Order_ID]) BETWEEN  '200622%' AND '200623%' )
    

    【讨论】:

      【解决方案5】:

      在您的 SQL 语句中包含 DATE(),因为您说您的列包含 datetime Like ...

      $query = "SELECT * FROM justshawarma_nexo_commandes where REF_CLIENT = '$uuc' 
          and DATE(DATE_CREATION) BETWEEN '$date3' and '$date4'";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-23
        • 2020-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多