MySQL可以用||连接字符串吗? [英] Can MySQL concatenate strings with ||

查看:938
本文介绍了MySQL可以用||连接字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用sqlite3,因此使用||运算符连接字符串.

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

稍后,我想转向MySQL,因此,如果不必更改代码,那就太好了.我通常会在MySQL中使用concat()进行连接. ||是否也可以工作,还是我必须修改我的代码?还是有其他解决方案?

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?

顺便说一下,我正在Ruby on Rails 3.1中进行编码.

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

推荐答案

||在MySQL中也可以使用,但是您需要将sql_mode设置为PIPES_AS_CONCAT.

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

官方文档

演示:

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)

这篇关于MySQL可以用||连接字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆