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时间戳之间的总时间。我试图使用联接来获得最后一个项目(条形码)签出或签入的员工。如果你拿出t3.employee的加入,并且只是要求员工,它给了我第一个记录员工条形码,而不是最后一个。
答案 0 :(得分:0)
您正在使用列条形码,但此列存在于多个表中,请尝试为每个条形码列分配表别名
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
答案 1 :(得分:0)
您在ON t1.barcode =条形码时缺少表格引用。由于条形码列在多个表中,因此需要严格识别,以免模糊不清。