所以我在SQL(postgres)中进行基本的连接查询...
SELECT first_name, last_name
FROM table1, table2
WHERE table1.id = table2.customer_id
Run Code Online (Sandbox Code Playgroud)
在返回的结果查询中,是否可以在返回的每一行中生成一个名为"default_value"的额外列,其字符串值为"test",如果是,如何?我不是要尝试使用新数据永久更改表,只需在结果集中添加一个额外的列.用例是将sql查询存储在Heroku dataclip中,以便生成csv报告.
a_h*_*ame 17
是的,这很容易:
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;
Run Code Online (Sandbox Code Playgroud)
您还应该在WHERE
子句中停止使用这些过时的隐式连接.请改用显式JOIN
运算符.它使查询更加健壮,防止意外忘记的连接条件.
“是否有任何选项可以对虚拟列的值设置条件?”
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;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11029 次 |
最近记录: |