1

以下查询运行良好:

SELECT * 
FROM MsUser 
WHERE absensi_tanggal = DATE_FORMAT( STR_TO_DATE( "' .$query_array['datepicker'] . '", \'%m/%d/%Y\' ) , \'%Y-%m-%d\' )

但是,此查询不起作用。我应该在那里错过一些东西:

SELECT * 
FROM MsUser 
WHERE absensi_tanggal BETWEEN DATE_FORMAT( STR_TO_DATE( "' .$query_array['datepicker1'] . '", \'%m/%d/%Y\' ) , \'%Y-%m-%d\' ) 
AND DATE_FORMAT( STR_TO_DATE( "' .$query_array['datepicker2'] . '", \'%m/%d/%Y\' ) , \'%Y-%m-%d\' )

上面的查询总是返回 0 结果。

任何帮助表示赞赏,谢谢:D

4

1 回答 1

1

由于STR_TO_DATE返回可与 BETWEEN 一起使用的正确 DATE 类型值,您是否简单地尝试过:

SELECT * 
FROM MsUser 
WHERE absensi_tanggal 
BETWEEN 
STR_TO_DATE( "' .$query_array['datepicker1'] . '", \'%m/%d/%Y\' )
AND 
STR_TO_DATE( "' .$query_array['datepicker2'] . '", \'%m/%d/%Y\' )

我的猜测是,MySQL 正在将前面的字符串转换为日期,然后再转换回字符串,然后再转换回日期以完成BETWEEN工作。

另外,试试这个让它更整洁一点:

"
SELECT * 
FROM MsUser 
WHERE absensi_tanggal 
BETWEEN 
STR_TO_DATE('{$query_array['datepicker1']}','%m/%d/%Y')
AND 
STR_TO_DATE('{$query_array['datepicker2']}','%m/%d/%Y');
"

万一您不确认 的值datepicker1早于datepicker2,您可以像这样运行查询以确保(尽管我建议仅用于测试):

SELECT * 
FROM MsUser 
WHERE 
(
absensi_tanggal 
BETWEEN 
STR_TO_DATE('{$query_array['datepicker1']}','%m/%d/%Y')
AND 
STR_TO_DATE('{$query_array['datepicker2']}','%m/%d/%Y')
) OR (
absensi_tanggal 
BETWEEN 
STR_TO_DATE('{$query_array['datepicker2']}','%m/%d/%Y')
AND 
STR_TO_DATE('{$query_array['datepicker1']}','%m/%d/%Y')
);
"
于 2013-09-15T13:42:18.817 回答