MySQL 添加一个带有值的临时列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3097150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:24:27  来源:igfitidea点击:

add a temporary column with a value

sqlmysql

提问by Asim Zaidi

I have a select statement like this

我有一个像这样的选择语句

select field1, field2  
  from table1

What I want is to have a newfield with only value "example".

我想要的是有一个只有值“example”的新字段。

newfielddoes not exist in the table, but when I run this query it kinda makes a virtual column with the values of example field.

newfield表中不存在,但是当我运行此查询时,它会创建一个包含示例字段值的虚拟列。

回答by Steve Homer

select field1, field2, 'example' as TempField
from table1

This should work across different SQL implementations.

这应该适用于不同的 SQL 实现。

回答by OMG Ponies

You mean staticly define a value, like this:

您的意思是静态定义一个值,如下所示:

SELECT field1, 
       field2,
       'example' AS newfield
  FROM TABLE1

This will add a column called "newfield" to the output, and its value will always be "example".

这将在输出中添加一个名为“newfield”的列,其值将始终为“example”。

回答by DA.

I'm rusty on SQL but I think you could use select as to make your own temporary query columns.

我对 SQL 生疏了,但我认为您可以使用 select as 来制作自己的临时查询列。

select field1, field2, 'example' as newfield from table1

That would only exist in your query results, of course. You're not actually modifying the table.

当然,那只会存在于您的查询结果中。您实际上并没有修改表格。

回答by DA.

select field1, field2, NewField = 'example' from table1 

回答by Dlorko

I giving you an example in wich the TABLE registrofaena doesn't have the column called minutos. Minutos is created and it content is a result of divide demora/60, in other words, i created a column to show the values of the delay in minutes.

我给你在至极表registrofaena没有列称为一个例子minutos。Minutos 被创建,它的内容是除以 demora/60 的结果,换句话说,我创建了一个列来显示以分钟为单位的延迟值。

This is the query:

这是查询:

SELECT idfaena,fechahora,demora, demora/60 as minutos,comentario 
FROM registrofaena  
WHERE fecha>='2018-10-17' AND comentario <> '' 
ORDER BY idfaena ASC;

This is the view:

这是视图:

This is the result

这是结果

回答by Raj Shekhar Singh

select field1, field2, '' as newfield from table1

This Will Do you Job

这会做你的工作