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;