MySQL5.7多表查询含相同字段时orderBy使用Convert函数的坑!
mysql版本:5.7
问题产生
执行的多表查询sql语句如下:
select table1.* from table1 join table2 where ... order by convert(column1 using 'gbk')
如果table1和table2中有相同字段(column1)一样,就会报错。
报错信息:Error 1052: Column 'column1' in order clause is ambiguous
如果这个sql语句中不使用convert函数,是能够指定表为table1的,不报错(因为select table1.*已经指定表)
select table1.* from table1 join table2 where ... order by column1
解决方法
convert函数中的value必须指定表
select table1.* from table1 join table2 where ... order by convert(table1.column1 using 'gbk')