简体   繁体   English

Concat一个字符串到SELECT * MySql

[英]Concat a string to SELECT * MySql

The following query works fine with MySQL: 以下查询适用于MySQL:

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

It Concat / to the selected title field. 它Concat /到选定的标题字段。

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一起使用

You simply can't do that in SQL. 你根本无法在SQL中做到这一点。 You have to explicitly list the fields and concat each one: 您必须明确列出字段并连接每个字段:

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. 如果您使用的是应用程序,则可以使用SQL读取列名称,然后使用您的应用程序构建如上所述的查询。 See this stackoverflow question to find the column names: Get table column names in mysql? 请参阅此stackoverflow问题以查找列名: 在mysql中获取表列名称?

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". * -syntax仅适用于“select * from”。 You can list the columns and construct the query dynamically though. 您可以列出列并动态构造查询。

You cannot concatenate multiple fields with a string. 您不能使用字符串连接多个字段。 You need to select a field instand of all ( * ). 您需要选择所有( * )的字段instand。

You cannot do this on multiple fields. 您不能在多个字段上执行此操作。 You can also look for this . 你也可以寻找这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM