1.GROUP_CONCAT函数:用于字段拼接比较合适,类似于foreach标签

 SELECT
    GROUP_CONCAT( NAME ORDER BY sex DESC SEPARATOR ',' ) 
FROM
    beauty

2.GROUP_CONCAT使用前和使用后对比

2.1使用前

    select distinct COLUMN_NAME  from information_schema.COLUMNS 
WHERE
column_name not in ('name') and
    table_name = 'beauty' 
    AND table_schema = 'girls';

2.2使用后:

 select group_concat(distinct COLUMN_NAME order by COLUMN_NAME ASC separator ',') from information_schema.COLUMNS 
WHERE
column_name not in ('name') and
    table_name = 'beauty' 
    AND table_schema = 'girls';
    

3.实战参考的

   begin
                                #查询该表的所有列用于拼接SQL
                                SET group_concat_max_len=102400;
                                select group_concat(distinct COLUMN_NAME order by COLUMN_NAME ASC separator ',')  into colName from `information_schema`.`COLUMNS` 
                                where TABLE_NAME  = c_a01_TblCod and COLUMN_NAME NOT IN('A00','RECORDID','T0196','T0196_01TIME') and table_schema = 'qyrlzy_avic_talent';
            end;

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐