Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在sql查询中添加具有默认值的自定义列?_Sql_Postgresql - Fatal编程技术网

如何在sql查询中添加具有默认值的自定义列?

如何在sql查询中添加具有默认值的自定义列?,sql,postgresql,Sql,Postgresql,所以我在SQL(postgres)中做一个基本的连接查询 在返回的结果查询中,是否可以在返回的每一行中生成一个名为“default_value”的额外列,该列的字符串值为“test”,如果可以,如何生成?我并没有试图用新数据永久地修改表,只是在结果集中添加一个额外的列。用例是将sql查询存储在Heroku数据剪辑中,以便生成csv报告。是的,这非常简单: select first_name, last_name, 'test' as default_value, -

所以我在SQL(postgres)中做一个基本的连接查询


在返回的结果查询中,是否可以在返回的每一行中生成一个名为“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;