这些表达完美无缺:
$sql = "SELECT * FROM TABLE_SUITE_INSPECTION
WHERE QA_DATE = '$s_date'; returns the data for the requested date
$sql = "SELECT * FROM TABLE_SUITE_INSPECTION
WHERE QA_DATE = '$s_date' OR QA_DATE='$e_date'"; returns the data for the two required dates.
我无法弄清楚这句话不起作用的原因:
$sql = "SELECT * FROM TABLE_SUITE_INSPECTION
WHERE QA_DATE BETWEEN '$s_date' AND '$e_date'";
SQL数据库的QA_DATE为VARCHAR。
返回的数据总是不完整或根本没有结果。
示例:数据库包含以下日期的数据:
2/4/18
2/5/18
2/7/18
2/8/18
2/9/18
2/11/18
2/15/18
2/16/18
2/18/18
The results for s_date=2/4/18 and e_date=2/18/18 are none.
The result for s_date=2/4/18 and e_date=2/4/18 is correct.
The result for s_date=2/18/18 and e_date=2/18/18 is correct.
The results for s_date=2/4/18 and e_date=2/5/18 are correct.
The results for s_date=2/4/18 and e_date=2/7/18 are correct.
The results for s_date=2/4/18 and e_date=2/8/18 are correct.
The results for s_date=2/4/18 and e_date=2/9/18 are correct.
The results for s_date=2/4/18 and e_date=2/11/18 are none.
The result for s_date=2/11/18 and e_date=2/11/18 is correct.
The results for s_date=Any date and e_date=2/9/18 are correct.
为什么查询没有获取大于2/11/18的日期的结果?
感谢您的帮助
答案 0 :(得分:0)
由于数据库列是varchar,因此按字母顺序比较数据。按字母顺序排列,2/11/18
是最早的值,这就是为什么你会看到你所看到的结果。这就是为什么始终最好为日期使用日期列类型的原因之一。
您可以使用https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date或类似功能使其正常工作。但是,如果您可以更改数据集,这将是一个更好的解决方案。