您的当前位置:首页正文

Column 'language' in where clause is ambiguous

2024-11-08 来源:个人技术集锦

该错误是在mybatis中由于查的多个表中都有同一个字段,而没有指定该字段是哪个表的,导致出现了模棱两可的情况
sql如下:

<select id="selectListByParam" resultMap="BaseResultDtoMap"> select pn.id,p.`name` as en_name,p.short_name,p.chinese_name, pn.notice_name,pn.notice_content,pn.notice_link,pn.language,pn.created_time,pn.project_id from project_notice pn join project p on pn.project_id = p.id <where> pn.deleted = 0 <if test="projectName!=null and projectName!= ''"> <bind name="_likeName" value="'%' + projectName + '%'"/> and (p.name like #{_likeName} or p.short_name like #{_likeName} or p.chinese_name like #{_likeName}) </if> <if test="language!=null and language!= ''"> and `language` = #{language} </if> </where> order by created_time desc </select>

然后导致报错:

### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'language' in where clause is ambiguous
### The error may exist in file [/Users/dingwei/Documents/workspace/huobi-news/hbn-pl-service/target/classes/mappers/ProjectNoticeMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT count(0) FROM project_notice pn JOIN project p ON pn.project_id = p.id WHERE pn.deleted = 0 AND `language` = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'language' in where clause is ambiguous
; ]; Column 'language' in where clause is ambiguous; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'language' in where clause is ambiguous
    at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:87)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
    at com.sun.proxy.$Proxy119.selectList(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
    at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139)
    at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76)

 

修改方法:只需要指定 'language'字段是哪张表即可

显示全文