Column ‘XXX‘ in where clause is ambiguous

338 阅读1分钟

这个错误的意思:这个字段不明确

当在java开发中遇到了Column ‘XXX (某字段)’ in where clause is ambiguous 问题时,一般都是多表查询的问题,应该多表查询的时候两张表中字段有相同的,在where条件中那个字段没有明确是那张表的字段,这时你需要去xml语句报错字段明确是哪张表的就OK了。

比如:drug_medical表有name ,drug_supplier 表有name ,而你想查询drug_medical的name

<select id="findPage" resultType="com.ak.springboot.entity.Medical">
    select drug_medical.*, drug_supplier.name as teacher  from drug_medical
    left join drug_supplier
    on drug_medical.sup_id = drug_supplier.id
    <where>
        <if test="name != null and name != ''">
            AND drug_medical.name like concat('%', #{name},'%')
        </if>
    </where>
</select>

倒数第四行明确了是drug_medical的name后,bug就解决了