SQL 在 select 语句中设置列的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3505603/
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
setting up a default value of a column in select statement
提问by abhinav singh
actually i have 2 tables table1 and table2
实际上我有 2 个表 table1 和 table2
table1
表格1
name
city
addr.
table2
表2
name
city
addr.
ph.no
now ph.no field is an extra field in table 2
现在 ph.no 字段是表 2 中的一个额外字段
so i want to show field ph.no with a default value of 12345 in the output of select query on table1 as i want to append that output into an outfile. help me out ..I am using db2 as400 database
所以我想在 table1 上选择查询的输出中显示默认值为 12345 的字段 ph.no,因为我想将该输出附加到输出文件中。帮帮我..我正在使用 db2 as400 数据库
回答by Mark Byers
Yes, you can do this:
是的,你可以这样做:
SELECT name, city, addr, 12345 AS ph_no
FROM table1
回答by user9600226
I know this thread is very old but in case someone needs an answer to Nimmagadda's question, you should be able to accomplish it with something along the lines of:
我知道这个线程已经很老了,但如果有人需要回答 Nimmagadda 的问题,你应该能够通过以下方式完成它:
SELECT name, city, addr, CASE name WHEN 'john' THEN 12345 WHEN 'peter' THEN 123 ELSE 0 /* ???? */ END AS ph_no FROM table1
SELECT name, city, addr, CASE name WHEN 'john' THEN 12345 WHEN 'peter' THEN 123 ELSE 0 /* ???? */ END AS ph_no FROM table1