Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

concatenation - Can MySQL concatenate strings with ||

I'm using sqlite3 for the moment, and hence concatenation strings using the || operator.

At some later date I'd like to shift to MySQL, and hence it would be nice if no changes to the code had to be made. I'd normally use concat() to concatenate in MySQL. Does || work too, or will I have to modify my code? Or is there any other solution?

I'm coding in Ruby on Rails 3.1, by the way.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The || works in MySQL as well but you need to set sql_mode to PIPES_AS_CONCAT.

Official Doc

Demo:

mysql> select c from tmp;
+------+
| c    |
+------+
| foo  |
| bar  |
+------+
2 rows in set (0.00 sec)

mysql> select c||' hi' from tmp;
+----------+
| c||' hi' |
+----------+
|        0 |
|        0 |
+----------+
2 rows in set, 2 warnings (0.00 sec)

mysql> set sql_mode=PIPES_AS_CONCAT;
Query OK, 0 rows affected (0.00 sec)

mysql> select c||' hi' from tmp;
+----------+
| c||' hi' |
+----------+
| foo hi   |
| bar hi   |
+----------+
2 rows in set (0.00 sec)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...