我在数据库上有date_from和date_to列...如何根据date_from和date_to显示所有记录作为范围日期

时间:2017-05-09 09:32:37

标签: php mysql

$productBrands = Brand::whereIn('brand_id', function ($query) use($searchTerm) {
$query->select('brand_id')
->from(with(new Product())->getTable())
->where(Product::getTableName().'.title', 'like', '%'.$searchTerm.'%');});

我希望根据数据库中date_from和date_to的输入显示所有记录。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

如果您需要此日期之间的范围,则可以使用BETWEEN。这是一个例子:

SELECT *
FROM order_details
WHERE order_date BETWEEN CAST('2014-02-01' AS DATE) AND CAST('2014-02-28' AS DATE);

答案 1 :(得分:1)

你可以用这种方式写作

SELECT la.emp_id AS  'EMPLOYEE ID', ta.surname AS SURNAME, ta.firstname AS FIRSTNAME, ta.middlename AS MIDDLENAME, la.department AS  'DEPARTMENT', la.leave_type AS  'LEAVE TYPE', la.date_from AS  'FROM DATE', la.date_to AS  'TO DATE', la.date_filed AS  'DATE FILED', la.num_days AS  'DAYS ABSENT', la.date_return AS  'DATE RETURN', la.reason AS  'REASON', ta.rem_vl AS  'REMAINING VL', ta.rem_sl AS  'REMAINING SL', la.remarks AS  'REMARKS'
FROM leave_application la
JOIN tsp_employee_account ta ON la.emp_id = ta.emp_id
WHERE DATE(la.date_from) >= '2017-04-22' -- <<--- check this
AND DATE(la.date_to) <= '2017-04-22' -- <<--- check this
AND la.filed !=  '0'
ORDER BY la.leave_id DESC 
LIMIT 0 , 30;