2013-02-10 65 views
1

當試圖連接某些值時,我不斷收到使用MySQL 5.5.27的錯誤。我搜索並看到了一堆charset的答案(無可否認,這是我頭上的TAD),但我已將所有表格轉換爲字符集utf8-unicode-ci,但仍然出現錯誤。MySQL串聯和排序錯誤的非法組合錯誤

當然有辦法連接這些值,但我只是不知道如何。我是一位Oracle相對較新的人。

這裏是SQL行:

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

我得到:

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

任何想法?

+0

也有類似的問題? http://stackoverflow.com/questions/6668989/how-can-i-force-the-value-of-a-mysql-query-to-use-a-particular-collat​​ion – 2013-02-10 20:07:23

+0

這一個也可以幫助:http: //stackoverflow.com/questions/7753608/illegal-mix-of-collat​​ions-for-operation-concat – 2013-02-10 20:15:34

+0

你的錢列是一個整數?如果不是,則無法使用格式,請參閱http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_format。 – 2013-02-10 23:07:51

回答

2

如果錢確實是一個VARCHAR中的數字,您可以使用cast。

試試這個:

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 . 

編輯

你應該首先嚐試:concat(pl.last_name,'-',format(money,0));

這是一個非常基本的PHP代碼,你可以使用。

<?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> 
+0

但我相信我的代碼正在吹響pl.last_name - 如果我沒有弄錯,那就是一直在「拉丁語」出現......並且爲了完全披露,並且背叛我缺乏PHP技能,最初我有一行返回「concat(pl.last_name,'
',format(money,0))」作爲交叉表查詢的「單元格」 - 將兩個值強制到我的單元格中的兩行上......但這就是離題...謝謝你的幫助! – user2059532 2013-02-10 23:37:18

+0

即我想我想讓我的pl.last_name出現「unicode」與
和整數中的「$」一起使用。 – user2059532 2013-02-10 23:38:47

+0

Oke,但你不能在這樣的查詢內混合使用php/html和mysql。你可以用輸出做到這一點,但不能用你發送給mysql服務器的查詢。 – 2013-02-10 23:42:44