我在mysql中给出的代码如下
select GROUP_CONCAT(table_name,'.',column_name,' as',table_name,'_',column_name) as column_list from INFORMATION_SCHEMA.COLUMNS where table_name in ('country_details','state_details', 'city_details');
我得到以下给出的结果,以city_details.id开头为 city_details_id ,..
结果(从 city_details,country_details,state_details 开始)
city_details.id作为city_details_id,city_details.city_name作为city_details_city_name,city_details.state_id作为city_details_state_id,country_details.id作为country_details_id,country_details.sortname作为country_details_sort,country_details_sort,country_details_sort ,state_details.state_name作为state_details_state_name,state_details.country_id作为state_details_country_id
预期结果(从 country_details,state_details,city_details 开始)
country_details.id作为country_details_id,country_details.sortname作为country_details_sortname,country_details.country_name作为country_details_country_name, country_details.phonecode为country_details_phonecode,state_details.id为state_details_id,state_details.state_name为state_details_state_name, state_details.country_id为state_details_country_id,city_details.id为city_details_id,city_details.city_name为city_details_city_name, city_details.state_id为city_details_state_id
我不知道如何解决此问题,因此任何人都请指导我解决此问题。
SELECT group_concat(table_name,'.',column_name,' as ',table_name,'_',column_name) as column_list FROM INFORMATION_SCHEMA.COLUMNS where table_name in ('country_details','state_details', 'city_details')ORDER BY FIELD(table_name,'country_details','state_details', 'city_details');
答案 0 :(得分:1)
查询末尾的ORDER BY不执行任何操作,因为查询仅包含一个结果行。 (任何具有COUNT或GROUP_CONCAT之类的聚合函数的查询将只包含一行,除非该查询具有GROUP BY子句。)
但是,您可以将ORDER BY放在GROUP_CONCAT聚合函数中。像这样:
SELECT GROUP_CONCAT(
table_name,'.',column_name,' as ',table_name,'_',COLUMN_NAME
ORDER BY FIELD(table_name,'country_details','state_details', 'city_details'),
ordinal_position
) AS column_list
FROM INFORMATION_SCHEMA.columns
WHERE table_name IN ('country_details','state_details', 'city_details')
(使用ordinal_position进行排序将每个表的列按它们在表定义中出现的顺序排列。)