SELECT barcode, t3.employee,
SEC_TO_TIME(SUM(TIMESTAMPDIFF(SECOND, dateandtime, (SELECT IFNULL(MIN(dateandtime),NOW())
FROM wwtlog b
WHERE b.barcode = a.barcode
AND b.dateandtime > a.dateandtime
AND b.inorout = 'IN'
)))) timeused
FROM wwtlog a
JOIN (SELECT t1.barcode, t1.employee FROM wwtlog t1
JOIN (SELECT barcode, MAX(id) id FROM wwtlog GROUP BY barcode) t2
ON t1.id = t2.id AND t1.barcode = t2.barcode) t3 ON t1.barcode = barcode
WHERE inorout = 'OUT' and barcode IN (SELECT t1.barcode FROM wwtlog t1
JOIN (SELECT barcode, MAX(dateandtime) dateandtime FROM wwtlog GROUP BY barcode) t2
ON t1.barcode = t2.barcode AND t1.dateandtime = t2.dateandtime WHERE inorout = 'IN')
GROUP BY barcode;
任何对此的帮助都会很棒,当我添加加入以获得 t3.employee 时,我开始得到这个。我有一种不知道自己在做什么的感觉。
我正在尝试获取唯一条形码,其中包含时间 IN 和时间 OUT 时间戳之间的总时间。我试图使用连接来获取最后 checkout 或 checkin 项目(条形码)的员工。如果您取出 t3.employee 的连接,并且只询问员工,它会为我提供每个员工的第一个记录员工条形码,不是最后一个。
最佳答案
您正在使用列条形码,但此列存在于多个表中尝试为每个条形码列分配表别名
SELECT a.barcode, t3.employee,
SEC_TO_TIME(SUM(TIMESTAMPDIFF(SECOND, dateandtime, (SELECT IFNULL(MIN(dateandtime),NOW())
FROM wwtlog b
WHERE b.barcode = a.barcode
AND b.dateandtime > a.dateandtime
AND b.inorout = 'IN'
)))) timeused
FROM wwtlog a
JOIN (SELECT t1.barcode, t1.employee FROM wwtlog t1
JOIN (SELECT barcode, MAX(id) id FROM wwtlog GROUP BY barcode) t2
ON t1.id = t2.id AND t1.barcode = t2.barcode) t3 ON t1.barcode = a.barcode
WHERE inorout = 'OUT' and a.barcode IN (SELECT t1.barcode FROM wwtlog t1
JOIN (SELECT barcode, MAX(dateandtime) dateandtime FROM wwtlog GROUP BY barcode) t2
ON t1.barcode = t2.barcode AND t1.dateandtime = t2.dateandtime WHERE inorout = 'IN')
GROUP BY a.barcode;
特别是在主表,例如:wwtlog a
关于mysql - 字段列表中的列 'barcode' 不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40289889/