MySQL 在 SELECT 语句中组合两个字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5291704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combining two fields in SELECT statement
提问by Bv202
In my table I have a field "firstname" and a field "lastname". I would like to select all records where firstname + space + lastname is a certain value.
在我的表中,我有一个字段“名字”和一个字段“姓氏”。我想选择 firstname + space + lastname 是特定值的所有记录。
I've tried this:
我试过这个:
$sql = "SELECT * FROM sam_users WHERE (user_firstname + ' ' + user_lastname LIKE ?)";
But this isn't working. With Google I've found something about using ||, but I don't really understand how I should use that operator. Note that I don't want to use an or-operator (what || is in many languages), but something to concatenate 2 fields (with a space between them) and using a LIKE on that.
但这不起作用。通过 Google,我发现了一些关于使用 || 的信息,但我真的不明白我应该如何使用该运算符。请注意,我不想使用 or 运算符(许多语言中的 || 是什么),而是要连接 2 个字段(它们之间有一个空格)并在其上使用 LIKE 的东西。
Thanks!
谢谢!
回答by outis
With MySQL, you can use CONCAT
:
使用 MySQL,您可以使用CONCAT
:
SELECT * FROM sam_users
WHERE CONCAT(user_firstname, ' ', user_lastname) LIKE ?
or CONCAT_WS
(which ignores NULL values):
或CONCAT_WS
(忽略 NULL 值):
SELECT * FROM sam_users
WHERE CONCAT_WS(' ', user_firstname, user_lastname) LIKE ?
However, MySQL won't be able to use any indiceswhen performing this query. If the value of the pattern argument to LIKE
begins with a wildcard, MySQL won't be able to use indices, so comparing to a generated value (instead of a column) won't make a difference.
但是,在执行此查询时,MySQL 将无法使用任何索引。如果模式参数 to 的值LIKE
以通配符开头,MySQL 将无法使用索引,因此与生成的值(而不是列)进行比较不会有什么不同。
You can also set the MySQL server SQL modeto "ANSI"or "PIPES_AS_CONCAT"to use the ||
operator for string concatenation.
您还可以将MySQL 服务器 SQL 模式设置为“ANSI”或“PIPES_AS_CONCAT”,以使用||
运算符进行字符串连接。
SET @@sql_mode=CONCAT_WS(',', @@sql_mode, 'PIPES_AS_CONCAT');
SELECT * FROM sam_users
WHERE (user_firstname || ' ' || user_lastname) LIKE ?
This sets the SQL mode for the current sessiononly. You'll need to set @@sql_mode
each time you connect. If you wish to unset 'PIPES_AS_CONCAT' mode in a session:
这仅为当前会话设置 SQL 模式。@@sql_mode
每次连接时都需要设置。如果您希望在会话中取消设置 'PIPES_AS_CONCAT' 模式:
SET @@sql_mode=REPLACE(@@sql_mode, 'PIPES_AS_CONCAT', '');
MySQL appears to remove any extra commas in @@sql_mode
, so you don't need to worry about them.
MySQL 似乎删除了 中的任何额外逗号@@sql_mode
,因此您无需担心它们。
Don't use SELECT *
; select only the columns you need.
不要使用SELECT *
; 仅选择您需要的列。
回答by Mark Byers
In SQL the ||
operator doesmean string concatenation according to the standard (see SQL 2008: 5.2). The boolean or operator is written OR
in SQL.
在 SQL 中,||
运算符确实表示根据标准进行字符串连接(请参阅 SQL 2008:5.2)。布尔或运算符是用OR
SQL编写的。
However not all databases implement it this way and so the exact syntax depends on the specific database.
然而,并非所有数据库都以这种方式实现它,因此确切的语法取决于特定的数据库。
回答by Pentium10
SELECT *
FROM sam_users
WHERE TRIM(Concat(user_firstname, ' ', user_lastname)) LIKE ?;