子查询中的未知列 - mysql

时间:2013-11-21 16:11:39

标签: mysql

我非常接近完成这个困难的查询。它很长,所以希望不要太过于压倒性的。但在我的案例中,我在select语句中引用了一个来自where语句的联合。它在'where子句'中给了我“MySQL数据库错误:未知列'U.EmpID'”。任何帮助将非常感激。这是查询:

SELECT U.EmpID,
   CASE
      WHEN     ((SELECT COUNT(*)
                   FROM (SELECT *
                           FROM timeclock_copy tp
                          WHERE     PunchEvent = 'breakin'
                                AND DATE(tp.PunchDateTime) =
                                      '2013-11-12'
                                AND tp.EmpID = U.EmpID) AS s) > 1)
           AND ((SELECT COUNT(*)
                   FROM (SELECT *
                           FROM timeclock_copy tp
                          WHERE     PunchEvent = 'breakout'
                                AND DATE(tp.PunchDateTime) =
                                      '2013-11-12'
                                AND tp.EmpID = U.EmpID) AS s) > 1)
      THEN
         "MULTIPLE BREAKS"
      ELSE
         "ONE BREAK"
   END
      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_copy 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_copy AS `leave`
          WHERE `leave`.`In-Out` = 0)) AS U
       LEFT JOIN testclb.prempl pe ON u.EmpID = pe.prempl
 WHERE DATE(U.`time`) >= '2013-11-12' AND DATE(U.`time`) < '2013-11-13'
GROUP BY date(U.`time`), EmpID
ORDER BY U.EmpID, U.`time` ASC

1 个答案:

答案 0 :(得分:0)

FROM子句中的子查询无法与外部语句相关联。我认为这就是为什么你在'where子句'中得到Unknown列'U.EmpID'“错误。