前言
使用 myBatis 在自定义查询语句时,使用联合查询出现报错问题解决。
报错
org.springframework.dao.DataIntegrityViolationException:
### Error querying database. Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'room_name' in field list is ambiguous
### The error may exist in file [E:\OpenSource\XidianReservation\target\classes\mapping\ReserveMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
主要报错看这条Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'room_name' in field list is ambiguous
原因
这个产生的原因有很多,具体根据报错查看。
SELECT
< include refid = "Base_Column_List" />
FROM
reserve,
room
WHERE
reserve_date = DATE_FORMAT( '2019-09-05', '%Y-%m-%d' )
AND reserve.room_name IN (
SELECT
room_name
FROM
room
WHERE
room.room_id = 1
)
我的报错主要是由于< include refid = "Base_Column_List" />
reserve中的roomName 与room中的roomName重叠,MySQL无法分辨。
解决方法
将 < include refid = "Base_Column_List" />
改为reserve的字段,并把重叠的加上 reserve.
即可。
Comments | NOTHING