如何将多列合并为一列并使用自定义字符串格式化?

发布于 2024-09-24 21:17:23 字数 262 浏览 1 评论 0原文

SELECT id, <X> AS name FROM `table`

基本上
的组合 姓氏 + ', ' + 名字

示例将是

   id | name        |
   2  | Smith, Bob  |
   3  | Jones, Susy |

这只是一个示例,我真的不想如此简单地组合名称。

SELECT id, <X> AS name FROM `table`

Basically <X> is a combination of
lastname + ', ' + firstname

example would be

   id | name        |
   2  | Smith, Bob  |
   3  | Jones, Susy |

This is just an example, I don't really want to combine names so simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄暮涼年 2024-10-01 21:17:23

CONCAT() 怎么样? 功能?

SELECT id, CONCAT(lastname, ', ', firstname) AS name FROM `table`;

如果要连接许多字段,您还可以考虑 CONCAT_WS() 函数,其中第一个参数是其余参数的分隔符,该分隔符添加在要连接的字符串之间:

SELECT id, 
       CONCAT_WS(',', field_1, field_2, field_3, field_4) list
FROM   `table`;

What about the CONCAT() function?

SELECT id, CONCAT(lastname, ', ', firstname) AS name FROM `table`;

If you are going to concatenate many fields, you could also consider the CONCAT_WS() function, where the first argument is the separator for the rest of the arguments, which is added between the strings to be concatenated:

SELECT id, 
       CONCAT_WS(',', field_1, field_2, field_3, field_4) list
FROM   `table`;
遗忘曾经 2024-10-01 21:17:23

使用 concat ,例如:

SELECT id, CONCAT(lastname, ' , ', firstname) AS name FROM `table`;

use concat like :

SELECT id, CONCAT(lastname, ' , ', firstname) AS name FROM `table`;
三五鸿雁 2024-10-01 21:17:23
SELECT 

CONCAT('https://example.com/estimation/create?pId=',task_p_id,'&estId=',task_est_id) as live_url,

CONCAT('http://stage.example.com/estimation/create?pId=',task_p_id,'&estId=',task_est_id) as stage_url 

FROM `ls_task` LEFT JOIN `ls_estimation` ON est_id=task_est_id LEFT JOIN `ls_project` ON p_id=task_p_id limit 10
SELECT 

CONCAT('https://example.com/estimation/create?pId=',task_p_id,'&estId=',task_est_id) as live_url,

CONCAT('http://stage.example.com/estimation/create?pId=',task_p_id,'&estId=',task_est_id) as stage_url 

FROM `ls_task` LEFT JOIN `ls_estimation` ON est_id=task_est_id LEFT JOIN `ls_project` ON p_id=task_p_id limit 10
蒲公英的约定 2024-10-01 21:17:23

您可以使用 GROUP_CONCAT():

获取以逗号分隔的表的所有列名的示例:

SELECT GROUP_CONCAT(c.`COLUMN_NAME`) FROM information_schema.`COLUMNS` c
WHERE c.`TABLE_SCHEMA` = "DB_NAME" AND c.`TABLE_NAME`="TABLE_NAME"

输出:

column_name_1,column_name_2,column_name_3,column_name_4,...

You can use GROUP_CONCAT():

Example of getting all the column names of a table separated by comma:

SELECT GROUP_CONCAT(c.`COLUMN_NAME`) FROM information_schema.`COLUMNS` c
WHERE c.`TABLE_SCHEMA` = "DB_NAME" AND c.`TABLE_NAME`="TABLE_NAME"

Output:

column_name_1,column_name_2,column_name_3,column_name_4,...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文