将字符串连接到 SELECT * MySql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13551041/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:33:02  来源:igfitidea点击:

Concat a string to SELECT * MySql

mysqlsqlstringconcat

提问by SaidbakR

The following query works fine with MySQL:

以下查询适用于 MySQL:

SELECT concat(title,'/') FROM `socials` WHERE 1

It Concat /to the selected title field.

它将/连接到选定的标题字段。

However, when I try to do the following:

但是,当我尝试执行以下操作时:

SELECT concat(*,'/') FROM `socials` WHERE 1

It returns the follwoing Error:

它返回以下错误:

 #1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near '*,'/') FROM `socials` WHERE 1 LIMIT 0, 30' at line 1

So is there any way to make such sql query to work with MySql

那么有什么方法可以使这样的 sql 查询与 MySql 一起使用

回答by Ben Lee

You simply can't do that in SQL. You have to explicitly list the fields and concat each one:

您根本无法在 SQL 中做到这一点。您必须明确列出字段并连接每个字段:

SELECT CONCAT(field1, '/'), CONCAT(field2, '/'), ... FROM `socials` WHERE 1

If you are using an app, you can use SQL to read the column names, and then use your app to construct a query like above. See this stackoverflow question to find the column names: Get table column names in mysql?

如果您使用的是应用程序,则可以使用 SQL 读取列名称,然后使用您的应用程序构建如上所示的查询。请参阅此 stackoverflow 问题以查找列名:Get table column names in mysql?

回答by Joni

If you want to concatenate the fields using / as a separator, you can use concat_ws:

如果要使用 / 作为分隔符连接字段,可以使用concat_ws

select concat_ws('/', col1, col2, col3) from mytable

You cannot escape listing the columns in the query though. The *-syntax works only in "select * from". You can list the columns and construct the query dynamically though.

但是,您无法避免列出查询中的列。*-语法仅适用于“select * from”。您可以列出列并动态构建查询。

回答by rekire

You cannot concatenate multiple fields with a string. You need to select a field instand of all (*).

您不能使用字符串连接多个字段。您需要在所有 ( *) 中选择一个字段。

回答by Rahul Tripathi

You cannot do this on multiple fields. You can also look for this.

您不能在多个字段上执行此操作。你也可以找这个