sql - 如何将新列添加到具有与同一个表对应的值的表中?

标签 sql postgresql

我的表中有一个状态列,其中包含 int 值。如何为 int 赋值,或者我是否必须在表中创建一个新列?

我已经尝试过 ALTER table 但最好的方法是什么?

select status from table1;

如果我运行上面的查询,我们得到 -

id status
1  1
2  2
3  1
4  5

我想得到输出-

id  status
1   Accepted
2   Completed
3   Accepted
4   Declined

最佳答案

使用 case expression , 邮政系统

select status,
       case 
         when status=1 then 'Accepted'
         when status=2 then 'Completed'
         when status=3 then 'Accepted'
         when sttaus=4 then 'Declined'
       end mystatus
from table1;

关于sql - 如何将新列添加到具有与同一个表对应的值的表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55492393/

相关文章:

SQL Server : concatenating values

mysql - postgresql 中的连接表

sql-server - PostgreSQL。创建包含连接其他列中的值的列的表

postgresql - RDS Postgres - 如何避免 'password' 语句进入日志文件

导入时的postgresql和排序规则问题

sql - SQL Server 中的动态透视列

python - 使用Python RE模块解析SQL语句

sql - 尽管数据库和服务器排序规则是 CI,但分组依据在 T-SQL 中区分大小写

postgresql - 如何在 PostgreSQL 中添加时间依赖触发器?

sql - Postgres/SQL 加入和更新