简体   繁体   English

选择*从表中,同时获取列名称作为mysql中的别名

[英]Select * From a table while getting a column name as alias in mysql

I would like to select all the columns in a table but one of the columns should be modified and have an alias. 我想选择表中的所有列,但其中一列应修改并具有别名。 Is there any better way to do this rather than writing all the column names and setting alias to the required one. 有没有比写入所有列名并将别名设置为所需名称更好的方法了。 Like 喜欢

SELECT
    *,
    date_format(a.date,'%m-%d-%y') AS formatDate
FROM
    myTable a
WHERE
    Id = 1;

I found a way to make this work. 我找到了一种使这项工作有效的方法。 We can select all and change the name of column(s) to alias. 我们可以选择全部并将列名更改为别名。

Let's say we have a table activity with following columns where startTime stores the timestamp with date and time and we need a coulmn that just gives us the date. 假设我们有一个表活动 ,其中包含以下各列,其中startTime存储带有日期和时间的时间戳,我们需要一个可以给我们提供日期的列。

sl.No  startTime  endTime  timeCreated
We can use this query 我们可以使用这个查询

SELECT *, date_format(startTime, '%m-%d-%Y') as startDate FROM activity which gives us. SELECT *, date_format(startTime, '%m-%d-%Y') as startDate FROM activity给我们的SELECT *, date_format(startTime, '%m-%d-%Y') as startDate FROM activity

sl.No startTime endTime timeCreated startDate

This will give us an extra column compared to the original table, but we don't have to list all the columns manually to set alias for just one column. 与原始表相比,这将为我们提供额外的列,但是我们不必手动列出所有列即可为一个列设置别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM