Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal mix of collations error in mysql query

Is there any way to compare the generated range column in the mysql query ?

SELECT ue.bundle,ue.timestamp,b.id,bv.id as bundleVersionId,bv.start_date,bv.end_date, bv.type,ue.type from (
 SELECT bundle,timestamp,tenant, case when Document_Id ='' then 'potrait'
 WHEN Document_Id<>'' then 'persisted' end   as type from uds_expanded ) ue
 JOIN bundle b on b.name=ue.bundle  join bundle_version bv on b.id=bv.bundle_id 
 WHERE ue.tenant='02306' and ue.timestamp >= bv.start_date and ue.timestamp <=bv.end_date and **ue.type=bv.type ;**

I am getting the following error when I try to compare the types

 Error Code: 1267. Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation '=' 0.000 sec
like image 627
Sandeep Rao Avatar asked Dec 15 '22 04:12

Sandeep Rao


2 Answers

Stick to one encoding/collation for your entire system. Right now you seem to be using UTF8 one place and latin1 in another place. Convert the latter to use UTF8 as well and you'll be good.

You can change the collation to UTF8 using

alter table <some_table> convert to character set utf8 collate utf8_general_ci;
like image 105
kba Avatar answered Jan 02 '23 16:01

kba


I think sometimes the issue is we use different orm utilities to generate table and then we want to test queries either by mysql command line or MySql workbench, then this problem comes due to differences of table collation and the command line or app we use. simple way is to define your variables (ones used to test the query against table columns)

ex:

MySQL>
MySQL> set @testCode = 'test2' collate utf8_unicode_ci;
Query OK, 0 rows affected (0.00 sec)

MySQL> select * from test where code = @testCode;

full details

like image 25
Tekz Avatar answered Jan 02 '23 16:01

Tekz