mysql 批量别名_mysql - 批量将别名应用于MYSQL中的表列 - 堆栈内存溢出

要使用这些表中的所有字段名生成字符串,可以查询information_schema.columns

例如:

SELECT

GROUP_CONCAT(CONCAT(TABLE_NAME,'.`',column_name,'` AS `',column_name,'_',replace(TABLE_NAME,'archive_day_',''),'`') SEPARATOR ',\r\n')

FROM information_schema.columns

WHERE TABLE_NAME like 'archive_day_%'

上dB的测试<>小提琴这里

并生成联接的然后您可以使用information_schema.tables

例如:

SELECT CONCAT('FROM (\r\n ',GROUP_CONCAT(CONCAT('SELECT `dateTime` FROM ',TABLE_NAME) SEPARATOR '\r\n UNION\r\n '),'\r\n) AS dt \r\nLEFT JOIN ',

GROUP_CONCAT(CONCAT(TABLE_NAME,' ON ',

TABLE_NAME,'.`dateTime` = dt.`dateTime`') SEPARATOR '\r\nLEFT JOIN ')) as SqlJoins

FROM information_schema.tables

WHERE TABLE_NAME like 'archive_day_%'

上dB的测试<>小提琴这里

对于它们将生成的2个示例表

archive_day_ET.`dateTime` AS `dateTime_ET`,

archive_day_ET.`min` AS `min_ET`,

archive_day_ET.`mintime` AS `mintime_ET`,

archive_day_ET.`max` AS `max_ET`,

archive_day_ET.`maxtime` AS `maxtime_ET`,

archive_day_ET.`sum` AS `sum_ET`,

archive_day_ET.`count` AS `count_ET`,

archive_day_ET.`wsum` AS `wsum_ET`,

archive_day_ET.`sumtime` AS `sumtime_ET`,

archive_day_inTemp.`dateTime` AS `dateTime_inTemp`,

archive_day_inTemp.`min` AS `min_inTemp`,

archive_day_inTemp.`mintime` AS `mintime_inTemp`,

archive_day_inTemp.`max` AS `max_inTemp`,

archive_day_inTemp.`maxtime` AS `maxtime_inTemp`,

archive_day_inTemp.`sum` AS `sum_inTemp`,

archive_day_inTemp.`count` AS `count_inTemp`,

archive_day_inTemp.`wsum` AS `wsum_inTemp`,

archive_day_inTemp.`sumtime` AS `sumtime_inTemp`

FROM (

SELECT `dateTime` FROM archive_day_ET

UNION

SELECT `dateTime` FROM archive_day_inTemp

) AS dt

LEFT JOIN archive_day_ET ON archive_day_ET.`dateTime` = dt.`dateTime`

LEFT JOIN archive_day_inTemp ON archive_day_inTemp.`dateTime` = dt.`dateTime`

你可能感兴趣的:(mysql,批量别名)