如何在sql查询中添加具有默认值的自定义列?
所以我在SQL(postgres)中做一个基本的连接查询如何在sql查询中添加具有默认值的自定义列?,sql,postgresql,Sql,Postgresql,所以我在SQL(postgres)中做一个基本的连接查询 在返回的结果查询中,是否可以在返回的每一行中生成一个名为“default_value”的额外列,该列的字符串值为“test”,如果可以,如何生成?我并没有试图用新数据永久地修改表,只是在结果集中添加一个额外的列。用例是将sql查询存储在Heroku数据剪辑中,以便生成csv报告。是的,这非常简单: select first_name, last_name, 'test' as default_value, -
在返回的结果查询中,是否可以在返回的每一行中生成一个名为“default_value”的额外列,该列的字符串值为“test”,如果可以,如何生成?我并没有试图用新数据永久地修改表,只是在结果集中添加一个额外的列。用例是将sql查询存储在Heroku数据剪辑中,以便生成csv报告。是的,这非常简单:
select first_name,
last_name,
'test' as default_value, --<< a "virtual" column containing a character value
42 as the_answer --<< another column containing a numeric value
from table1
join table2 on table1.id = table2.customer_id;
选择名字,
姓,
“test”作为默认值,--“是否有任何选项可以对虚拟列的值设置条件?”
有人知道为什么这篇文章被否决了吗?有没有办法对虚拟专栏的价值设置一个条件,bdw,这是一个很大的帮助
select first_name,
last_name,
'test' as default_value, --<< a "virtual" column containing a character value
42 as the_answer --<< another column containing a numeric value
from table1
join table2 on table1.id = table2.customer_id;
select first_name,
last_name,
CASE WHEN first_name = 'Mary' THEN 'test' WHEN first_name = 'John' THEN 'test2'
ELSE 'Not known' END as default_value,
42 as the_answer
from table1
join table2 on table1.id = table2.customer_id;