[Err] 1052 - Column 'employee_number' in field list is ambiguous 执行报错了怎么办
时间: 2024-04-21 22:22:16 浏览: 182
这个错误是因为查询中的字段名存在歧义,数据库无法确定是哪个表的字段。在你的查询中,涉及到多个表的employee_number字段,导致了这个错误。
为了解决这个问题,你可以在查询中明确指定每个字段的表名或表别名,以消除歧义。例如,假设gate_staff表的employee_number字段需要指定别名为"staff_employee_number":
```sql
SELECT ccc.employee_number, ccc.card_number
FROM (
SELECT mid.id, mid.employee_number, mid.card_number
FROM (
SELECT CASE
WHEN LENGTH(pin) = 5 THEN CONCAT('000', pin)
ELSE pin
END AS employee_number,
cardno AS card_number,
'' AS id
FROM gate_staff
UNION ALL
SELECT employee_number, card_number, id
FROM gate_mdm_dm_cardinfo a
INNER JOIN (
SELECT employee_number, MAX(last_update_time) max_date
FROM gate_mdm_dm_cardinfo
WHERE last_update_time >= #{startTime} AND last_update_time <= #{endTime}
GROUP BY employee_number
) b ON a.employee_number = b.employee_number AND a.last_update_time = b.max_date
WHERE a.status = 1 AND a.public_flag = 'Y'
) AS mid
GROUP BY mid.employee_number, mid.card_number
HAVING COUNT(*) = 1
) AS ccc
WHERE ccc.id != '';
```
在这个例子中,我使用了ccc作为中间结果集的别名,并在最外层的SELECT语句中明确指定了字段的表名或别名。你可以根据实际情况对其他字段进行相似的处理。
请注意,根据你的表结构和需求,你可能需要适当调整别名或表名以匹配你的实际情况。
阅读全文