Weird behaviour of SUM and CONCAT in MySql

后端 未结 1 1758
谎友^
谎友^ 2020-12-17 21:56

If I want to make a sum of a specific numeric column in MySQL, I do

SELECT SUM(MyColumn) FROM MyTable WHERE 1;

This returns for example num

相关标签:
1条回答
  • 2020-12-17 22:24

    As FreshPrinceOfSO suggested in the comments below my question, MySQL server doesn't handle casts to varchar.

    So even though the query

    SELECT CONCAT('Sum is: ',CAST(SUM(MyColumn) AS varchar(20))) FROM MyTable WHERE 1;
    

    results in syntax error, casting to char instead works just fine:

    SELECT CONCAT('Sum is: ',CAST(SUM(MyColumn) AS char(20))) FROM MyTable WHERE 1;
    
    0 讨论(0)
提交回复
热议问题