skip to Main Content

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!

2

Answers


    1. String Literal Concatenation: In MySQL, if you place string literals
      next to each other without commas or operators, MySQL implicitly
      concatenates them. In your case, ‘a’ ‘b’ ‘c’ is interpreted as one
      continuous string, resulting in ‘abc’.
    2. Column Name: Since the first literal (‘a’) is treated as the "alias"
      for the column, the result is that ‘a’ becomes the column name.
    Login or Signup to reply.
  1. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search