数据库表查询-对查询的列进行操作

2014-11-24 17:07:20 · 作者: · 浏览: 0

\

如上表所示,如何将num字段的YES变成Y,而NO变成N,即如下图:

\

SQL:(postgresql)

select id,
case
    when num = 'YES' then 'Y'
    when num = 'NO' then 'N'
    else 'error'
end as result
from example

数据库表结构:

CREATE TABLE example
(
  id integer NOT NULL,
  num character varying(10),
  CONSTRAINT example_pkey PRIMARY KEY (id)
)