我有一个问题如何在MySQL查询中为日期范围添加查询。日期仅生成。
这是我的查询:
SELECT TS.id AS TSid, employeeid, `date` AS date_d, TS.TI1 AS TI1, TS.TO1 AS TO1,
CONCAT(E.lastName,', ',E.firstName,' ',E.middleInitial,'.') AS 'fullname',
(TIME_TO_SEC(CAST(TS.`TI1` AS time))/3600) AS 'timeinint',
(TIME_TO_SEC(CAST(TS.`TO1` AS time))/3600) AS 'timeoutint',
E.startDate, C.companyName, B.branchName,
R.mon, R.tue, R.wed, R.thu, R.fri, R.sat, R.sun
FROM timeschedule AS TS
LEFT JOIN employee AS E ON TS.employeeid = E.id
LEFT JOIN company AS C ON E.companyid = C.id
LEFT JOIN branch AS B ON E.branchid = B.id
LEFT JOIN restday AS R ON R.id = E.restDayid
WHERE TS.`show` = '1'
ORDER BY employeeid ASC
我是如何获得输入的:
$mfrom = $this->input->get('mfroma'); //From: Month
$yfrom = $this->input->get('yfroma'); //From: Year
$yto = $this->input->get('ytoa'); //To : Year
答案 0 :(得分:0)
使用您指定的字段(从月份,年份到年份),您可以将这些条件添加到WHERE
子句中。我将在此示例中使用E.startDate
字段:
$sql = "-- your query here...
WHERE
TS.`show` = '1'
AND MONTH(E.startDate) >= " . $mfrom . "
AND YEAR(E.startDate) BETWEEN " . $yfrom . " AND " . $yto . "
ORDER BY employeeid ASC";
注意: PHP解析双引号内的变量,但我个人更喜欢将PHP变量保留在它们之外。