首先,我正在运行PostgreSQL 8.2,并在pgAdmin上测试我的查询。
我有一张表格,其中包含一些字段,例如:
mytable(
id integer,
mycheck boolean,
someText varchar(200));
现在,我想要一个类似于这样的查询:
select id,
case when mycheck then (select name from tableA)
else (select name from tableB) end as mySpecialName,
someText;
我尝试运行并得到了以下结果:
ERROR: CASE types character varying and boolean cannot be matched
SQL state: 42804
甚至尝试用以下方式欺骗PostgreSQL:
case (mycheck::integer) when 0 then
没有起作用。
所以,我的问题是:由于SQL没有if,只有case,那么我该如何使用布尔字段进行if操作?