重命名 SQL SELECT 语句中的列 [英] Renaming Columns in an SQL SELECT Statement

查看:79
本文介绍了重命名 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'

这篇关于重命名 SQL SELECT 语句中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆