无法在MYSQL语句中正确选择FROM日期

时间:2017-08-06 07:16:10

标签: php mysql

  $from = "2017-7-1";
  $to = "2017-8-25";

我不知道为什么我似乎无法正确选择 HTML5日期选择器中的从日期,例如当我选择2017-7-1时,结果页面不会在2017-7-1上显示结果数据, 但是当我选择2017-6-30 一天之前时,它可以显示。 知道为什么吗? 我尝试了BETWEEN CLause和> = AND< =, 他们都不工作 知道为什么吗?感谢

  $result = mysql_query("SELECT * FROM treatmentdetail WHERE nextdate BETWEEN 
  '$from' AND '$to'");

  $result = mysql_query("SELECT * FROM treatmentdetail WHERE nextdate >= 
  '$from' AND nextdate <= '$to'");

3 个答案:

答案 0 :(得分:1)

You should convert properly to date

   SELECT * 
   FROM treatmentdetail 
   WHERE str_to_date(nextdate, '%Y-%m-%d') 
      BETWEEN str_to_date('$from','%Y-%m-%d' ) AND  str_to_date('$to','%Y-%m-%d' ) 

and you should use most recent mysql driver .. with param binding too

答案 1 :(得分:0)

您缺少连接变量 我想你将不得不使用连接变量。

$result = mysqli_query($conn,"SELECT * FROM treatmentdetail WHERE nextdate BETWEEN 
  '$from' AND '$to'") or die(mysqli_error($conn));

答案 2 :(得分:0)

将您的字段更改为DATE和 将您的日期保存为YYYY-MM-DD,如您所示

  $from = "2017-07-01";
  $to = "2017-08-25";

您可以使用mysql(i)正确搜索

SELECT * FROM treatmentdetail 
    WHERE 
        nextdate >= '$from' AND nextdate <= '$to';

查看文档

MySQL DATATYPES DATE