我在为我的代码执行正确的语法时遇到了一些麻烦。我总结一下员工在特定周工作的小时数,但我想从查询中排除某些天数。员工可以为不同的客户工作,这些客户由client_id定义。
现在下面的代码在parantheses内的WHERE语句中有一个语法错误,我知道为什么,我只是让他们在那里为了解释我想在这种情况下实现的目标。
我感谢你能给我的任何帮助。
顺便说一句:我也尝试了AND(DAYOFWEEK(日期)<> 7 AND client_id ='1')但这也不起作用。我重新判断这是因为我的查询只返回一行结果,因此我的上述代码在查询循环时不执行任何操作
SELECT
date,
client_id,
SUM(hours)-37.5 AS total,
SUM(lunch = 'yes') AS lunch
FROM
hours
WHERE
CONCAT(WEEK(date, 1)) = '25'
AND
CONCAT(YEAR(date)) = '2015'
AND
employee_id = '14'
AND ( DAYOFWEEK(date) <> 7 WHERE client_id = '1' )
AND ( DAYOFWEEK(date) <> 1 WHERE client_id = '2' )
AND
status = 'billed'
HAVING SUM(hours) > 37.5
答案 0 :(得分:0)
您可以使用一堆逻辑not
和and
逻辑运算符来构建此逻辑:
SELECT
date,
client_id,
SUM(hours)-37.5 AS total,
SUM(lunch = 'yes') AS lunch
FROM
hours
WHERE
CONCAT(WEEK(date, 1)) = '25'
AND
CONCAT(YEAR(date)) = '2015'
AND
employee_id = '14'
AND NOT ( DAYOFWEEK(date) = 7 AND client_id = '1' )
AND NOT ( DAYOFWEEK(date) = 1 AND client_id = '2' )
AND
status = 'billed'
HAVING SUM(hours) > 37.5