SELECT id, <X> AS name FROM `table`
基本上<X>
是
的组合
姓氏+','+名字
例子是
id | name |
2 | Smith, Bob |
3 | Jones, Susy |
这只是一个例子,我真的不想将名称组合起来这么简单。
答案 0 :(得分:69)
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`;
答案 1 :(得分:3)
使用concat之类的:
SELECT id, CONCAT(lastname, ' , ', firstname) AS name FROM `table`;
答案 2 :(得分:1)
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
答案 3 :(得分:-1)
您可以使用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,...