mysql - 字段列表中的列 'barcode' 不明确

标签 mysql

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/

相关文章:

php - PDO更新不起作用,没有错误

php - 检查 php 数组中是否存在值 - 不起作用?

php - MySQL,查找 "manager"的所有员工

java - JPA + MySql + 实现加密密码

MYSQL 奇怪的语法错误,带/不带分号

mysql - Rails Mysql Group by strftime 查询在生产中不起作用

PHP MYSQL 多个下拉菜单选择更改值

mysql - 测量 Apache/Mysql 服务器上的内存使用情况

CREATE TRIGGER 附近的 MySQL 触发器语法错误

mysql - SQL Query Distinct Binary 替换为日期列