我的脚本在这里。这是我的查询,我使用paint来显示我的错误:
我想要的输出是从计划表中选择工作日名称='星期六'左连接到时间表所以
示例今天的日期是7/29/2017然后=星期六
SELECT effectiveDate,
CASE
WHEN (SELECT DATE_FORMAT(effectiveDate, '%W') = 'Saturday'
FROM schedules LEFT JOIN timesheet ON schedules.empid = timesheet.empid
AND timesheet.date = schedules.effectiveDate GROUP BY effectiveDate)
THEN 'Saturday'
END
FROM schedules
LEFT JOIN timesheet ON schedules.empid = timesheet.empid AND timesheet.date = schedules.effectiveDate
WHERE effectiveDate <= CURDATE()
AND YEAR(effectiveDate) = YEAR(CURDATE())
AND MONTH(effectiveDate) = MONTH(CURDATE()) AND schedules.empid = '40'
GROUP BY effectiveDate
答案 0 :(得分:0)
您不需要子查询(与主查询相同),只需直接引用该列。
此外,您可以使用DAYNAME
功能获取当天的名称,而不是DATE_FORMAT
。
SELECT effectiveDate,
CASE
WHEN DAYNAME(effectiveDate) = 'Saturday'
THEN 'Saturday'
END AS day
FROM schedules
LEFT JOIN timesheet ON schedules.empid = timesheet.empid AND timesheet.date = schedules.effectiveDate
WHERE effectiveDate <= CURDATE()
AND YEAR(effectiveDate) = YEAR(CURDATE())
AND MONTH(effectiveDate) = MONTH(CURDATE()) AND schedules.empid = '40'
GROUP BY effectiveDate
答案 1 :(得分:0)
您不需要子查询。您可以在选择查询中直接访问effectiveDate并执行相同的操作。
SELECT effectiveDate,
CASE
WHEN DATE_FORMAT(effectiveDate, '%W') = 'Saturday'
THEN 'Saturday'
ELSE 'Something else'
END AS day
FROM schedules
LEFT JOIN timesheet ON schedules.empid = timesheet.empid AND timesheet.date = schedules.effectiveDate
WHERE effectiveDate <= CURDATE()
AND YEAR(effectiveDate) = YEAR(CURDATE())
AND MONTH(effectiveDate) = MONTH(CURDATE()) AND schedules.empid = '40'
GROUP BY effectiveDate