MySQL concatenation and Illegal mix of collations error

蹲街弑〆低调 提交于 2019-12-08 06:31:49

问题


I keep getting an error using MySQL 5.5.27 when trying to concatenate some values. I've searched and seen a bunch of charset answers (which admittedly is a TAD over my head), but I've converted all my tables to Charset utf8-unicode-ci and still get the error.

Surely there is a way to concatenate these values, but I just don't know how. I'm an Oracle guy that is relatively new to MySQL.

Here is the SQL line:

concat(pl.last_name,'-',format(money,0))

I get:

#1270 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT), (utf8_unicode_ci,COERCIBLE), (utf8_unicode_ci,COERCIBLE) for operation 'concat'

Any ideas?


回答1:


If money is indeed a number inside a VARCHAR you could use cast.

Try this:

concat_ws(pl.last_name,'-',cast(money AS unsigned)); // This is with decimals.
concat(`pl.last_name,'-',substring_index(money,',',1)) // Without decimals. If you use . i.e. the American currency notation you can substitute , with an .   

Edit

Your should first try: concat(pl.last_name,'-',format(money,0));

This a very basic php code you could use.

<?php

function selecting_data(){

    $host = "host";
    $user = "username";
    $password = "password";
    $database = "database";
    $charset = "utf8";

    $link = mysqli_connect($host, $user, $password, $database);
    mysqli_set_charset($charset, $link);
    IF (!$link) {
        echo('Unable to connect to the database!');
    } ELSE {


        $query = "SELECT lastname, format(money,0) FROM mytable"; //Select query
        $result = mysqli_query($link, $query);
        while ($rows = mysqli_fetch_array($result, MYSQLI_BOTH)){
            echo $rows['lastname']."<br>".$rows['money'] ; 
        }
    }
    mysqli_close($link);


}
?>

<html>
<head><title>title</title></head>
<body>
<?PHP echo selecting_data(); ?>
</body>
</html>


来源:https://stackoverflow.com/questions/14801998/mysql-concatenation-and-illegal-mix-of-collations-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!