How to combine multiple columns as one and format with custom strings?

前端 未结 4 1041
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 16:21
SELECT id,  AS name FROM `table`

Basically is a combination of
lastname + \', \' + firstname

example wo

相关标签:
4条回答
  • 2020-12-14 16:22

    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`;
    
    0 讨论(0)
  • 2020-12-14 16:28

    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,...
    
    0 讨论(0)
  • 2020-12-14 16:39
    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
    
    0 讨论(0)
  • 2020-12-14 16:45

    use concat like :

    SELECT id, CONCAT(lastname, ' , ', firstname) AS name FROM `table`;
    
    0 讨论(0)
提交回复
热议问题