重命名 SQL SELECT 语句中的列

重命名 SQL SELECT 语句中的列

问题描述:

我想重命名 SELECT 表达式结果中的列.当然,以下方法不起作用:

I would like to rename the columns in the results of a SELECT expression. Of course, the following doesn't work:

SELECT * AS foobar_* FROM `foobar`

由于我是 SQL 新手,我认为我只是缺少一个可以找到答案的概念、工具或关键字.正确方向的提示将不胜感激.谢谢!

As I'm new to SQL, I think I'm just missing a concept, tool, or keyword that would lead to the answer. A hint in the right direction would be appreciated. Thanks!

更新

我正在寻找一种通用的方法来做到这一点,而特定于 MySQL 的技术绝对没问题.

I'm looking for a generic way to do this, and MySQL-specific techniques are absolutely fine.

简而言之,我正在编写一个工具,将 MySQL 查询的结果导出"到 Google 电子表格(通过 Google 数据 API).有些查询是连接,因此为了使列唯一,我想在所有列名前加上它们各自的表名.

In short, I'm writing a tool that "exports" the results of MySQL queries to Google Spreadsheets (via the Google Data API). Some queries are joins, so to make columns unique I wanted to prefix all column names with their respective table names.

你可以像这样一一为列名设置别名

You can alias the column names one by one, like so

SELECT col1 as `MyNameForCol1`, col2 as `MyNameForCol2` 
FROM `foobar`

编辑您可以直接访问 INFORMATION_SCHEMA.COLUMNS 以像这样修改新别名.但是,如何将其放入查询中超出了我的 MySql 技能 :(

Edit You can access INFORMATION_SCHEMA.COLUMNS directly to mangle a new alias like so. However, how you fit this into a query is beyond my MySql skills :(

select CONCAT('Foobar_', COLUMN_NAME)
from INFORMATION_SCHEMA.COLUMNS 
where TABLE_NAME = 'Foobar'