MySQL DateDiff in Where clause?

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

We are using Yii Framework and have created a search form to include the following DatePicker widge:

<?php echo $form->labelEx($model, 'availability_start_date');?>         <?php          Yii::import('zii.widgets.jui.CJuiDatePicker');         $this->widget('zii.widgets.jui.CJuiDatePicker', array(             'name'=>'stattDate',             'options'=>array(                 'showAnim'=>'fold',             ),             'htmlOptions'=>array(                 'style'=>'height:20px;'             )         ));         ?> 

This widget needs to be used to search for users who have an availability_start_date +/- 2 days from the value specified in the above widget.

Our UserController has the following join logic:

if ($search_frm['availability_start_date']){                     $join .= ' LEFT JOIN user_availability_start_date usd on u.id = usd.id';                     $where .= 'AND usd.availability_start_date >= '.$search_frm                 ['availability_start_date'];                 } 

Currently the logic in the WHERE clause just asks for a match where the availability_start_date is greater than or equal to the widget's value.

How do I modify the above WHERE clause to select those records where the widget's value is +/- 2 days different than the availability_start_date value?

UPDATE: I've revised the where clause to read as follows:

if ($search_frm['availability_start_date']){                     $join .= ' LEFT JOIN user_availability_start_date usd on u.id = usd.id';                     $where .= ' AND usd.availability_start_date >= DATE_ADD('.$search_frm.',                  INTERVAL -2 DAY)                     AND usd.availability_start_date <= DATE_ADD(' .$search_frm.', INTERVAL 2 DAY)';                 } 

Unfortunately, when I test the logic, records are returned that don't meet this criteria. No errors are thrown on the form or page.

回答1:

DATEDIFF() returns a value in days. For +/- 2 days you will want to use the absolute value ABS() returned by DATEDIFF(), verify that it is <= 2.

$where .= "AND ABS(DATEDIFF(DATE(usd.availability_start_date), DATE('" . $search_frm['availability_start_date'] . "'))) <= 2"; 

I have also wrapped the arguments to DATEDIFF() in DATE() to truncate off the time portions if they are present. Remove those if they are unnecessary for your application.



回答2:

BETWEEN would get that done for you.

"AND usd.availability_start_date      BETWEEN DATE_ADD('".         $search_frm['availability_start_date'] ."', INTERVAL - 2 DAY)     AND DATE_ADD('".         $search_frm['availability_start_date'] ."', INTERVAL 2 DAY)"; 


回答3:

AND usd.availability_start_date >= DATE_ADD('.$search_frm .', INTERVAL -2 DAY) AND usd.availability_start_date <= DATE_ADD('.$search_frm .', INTERVAL 2 DAY) 


回答4:

Try this:

if ($search_frm['availability_start_date']) {     $join .= ' LEFT JOIN user_availability_start_date usd on u.id = usd.id';     $where .= sprintf(         'AND %s BETWEEN (usd.availability_start_date - INTERVAL 2 DAY) '         . ' AND (usd.availability_start_date + INTERVAL 2 DAY)',         $search_frm['availability_start_date']     ); } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!