Search code examples
mysqlselectconcatenation

mysql select query result - auto concat?


I unexpectedly executed the following query in MySQL and got a strange result:

SELECT 'a' 'b' 'c' FROM dual;

The result shows 'a' as the column name and 'abc' as the value. I am puzzled by this behavior. What is the reason for this?

Feel free to ask if you need any more assistance!

It seems that when using simple constant strings, concatenation occurs. However, I found that if a different type (such as a number) or a database column is included in between, an error occurs.

Let me know if you need anything else!


Solution

  • https://dev.mysql.com/doc/refman/8.0/en/string-literals.html

    mysql> SELECT 'hello', '"hello"', '""hello""', 'hel''lo', '\'hello';
    +-------+---------+-----------+--------+--------+
    | hello | "hello" | ""hello"" | hel'lo | 'hello |
    +-------+---------+-----------+--------+--------+
    
    mysql> SELECT "hello", "'hello'", "''hello''", "hel""lo", "\"hello";
    +-------+---------+-----------+--------+--------+
    | hello | 'hello' | ''hello'' | hel"lo | "hello |
    +-------+---------+-----------+--------+--------+
    

    This maybe help.