重命名 SQL SELECT 语句中的列

编程入门 行业动态 更新时间:2024-10-22 17:39:15
本文介绍了重命名 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 语句中的列

本文发布于:2023-10-16 16:24:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498087.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   重命名   SQL   SELECT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!