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!
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.
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’. - Column Name: Since the first literal (‘a’) is treated as the “alias”
for the column, the result is that ‘a’ becomes the column name.
1