我有一个超长的复杂 mysql 查询,我可以在这里粘贴,但我认为这对我的问题没有帮助。基本上,我有一个子查询,有时会返回不止一行。当这种情况发生时,整个查询就会爆炸。所以我想做的是让子查询返回该列的正常结果,除非返回多行,然后在该列中输出一些指定的文本。有道理?
编辑:根据要求,我将发布我的大型查询。我可能这样做很困难,但是我花了很长时间才得到查询,所以我不知道从哪里开始重写它。
SELECT TIME_FORMAT(
TIMEDIFF(
(SELECT PunchDateTime
FROM timeclock_punchlog tp
WHERE PunchEvent = 'breakin'
AND DATE(tp.PunchDateTime) = DATE(U.time)
AND tp.EmpID = U.EmpID),
(SELECT PunchDateTime
FROM timeclock_punchlog tp
WHERE PunchEvent = 'breakout'
AND DATE(tp.PunchDateTime) = DATE(U.time)
AND tp.EmpID = U.EmpID)), '%i min.') AS Lunch
FROM ((SELECT `enter`.EmpID,
`enter`.PunchDateTime AS `time`,
DATE_FORMAT(`enter`.PunchDateTime, '%m-%d-%Y')
AS 'Punch Date',
TIMESTAMPDIFF(SECOND, `enter`.PunchDateTime, '2003-05-01 00:00:00')
AS `delta`
FROM timeclock_punchlog AS `enter`
WHERE `enter`.`In-Out` = 1)
UNION
(SELECT `leave`.EmpID,
`leave`.PunchDateTime AS `time`,
DATE_FORMAT(`leave`.PunchDateTime, '%m-%d-%Y')
AS 'Punch Date',
-TIMESTAMPDIFF(SECOND, `leave`.PunchDateTime, '2003-05-01 00:00:00')
AS `delta`
FROM timeclock_punchlog AS `leave`
WHERE `leave`.`In-Out` = 0)) AS U
LEFT JOIN prempl pe ON u.EmpID = pe.prempl
WHERE DATE(U.`time`) >= ?startDate
AND DATE(U.`time`) < ?endDate
GROUP BY date(U.`time`), EmpID
ORDER BY U.EmpID, U.`time` ASC";
有时返回多于一行的部分是定义为“午餐”的查询。