简体   繁体   中英

MySQL Set collate by a variable

to avoid a "mix of illegal colletions" while updating databases on different systems I want to do something like that:

    SELECT @collcation := TABLE_COLLATION FROM information_schema.tables WHERE table_schema = "information_schema" AND TABLE_NAME = "COLUMNS";
    SELECT * FROM ... WHERE COLUMN_NAME="extended" COLLATE @collcation;

Is it possible to set the COLLATE from a variable?

Here my example:

select @collcation := TABLE_COLLATION
from information_schema.tables
where table_schema='testdrive_unit_test' and TABLE_NAME = "car"
;
+--------------------------------+
| @collcation := TABLE_COLLATION |
+--------------------------------+
| latin1_swedish_ci              |
+--------------------------------+

select * from car order by model collate @collcation
;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@collcation' at line 1

select * from car order by model collate latin1_swedish_ci limit 3
;
+----+----------+-------+----------+
| id | brand_id | model | maxSpeed |
+----+----------+-------+----------+
| 14 |        5 | 301   |      160 |
| 15 |        5 | 401   |      190 |
| 16 |        5 | 407   |      210 |
+----+----------+-------+----------+
3 rows in set (0.00 sec)

In this opinion i think that it isn't possible set the collate from a variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM