doudouwen2763 2015-10-23 10:38
浏览 69
已采纳

在查询中使用多个条件时出错

I am working with PostgreSQL 8.4.4. I want to fetch record from table based on multiple conditions as,

1) check if record exists

2) if exists then -> if type_id is 1 then select name if type_id is 2 then select name, abbreviation

3) if record not exits then print as 'Missing'

Following is the query for the same

select 
    case 
        when exists (select NAME || ' ' || ABBREVIATION from CONTACTS_COMPANY where id = 15)
        then ( select
                case
                    when type_id = '1' 
                        then (select UPPER(NAME) from CONTACTS_COMPANY where id = 15) 
                    when type_id = '2' 
                        then (select UPPER(NAME) || ' ' || UPPER(ABBREVIATION) from CONTACTS_COMPANY where id = 15) 
                end
            ) 
    else UPPER('MISSING')
end

When firing above query I am getting error as

SQL error:

ERROR: column "type_id" does not exist

LINE 7: when type_id = '1'

I am not getting what exactly is causing error. Any help would be appreciated. Thanks in advance!

  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 VB6.0使用WebBrowser控件打开网页总是弹出js错误提示,请解决。
      • ¥20 如何按照企业名字进行爬虫
      • ¥15 关于#单片机波形发生器频率计算#的问题,如何解决?
      • ¥100 制作短链接和api接口的
      • ¥15 星河亮点sp9500测试指导
      • ¥15 dataframe 某列数据分列
      • ¥15 风扇导入fluent后仿真压力和速度数据卡在旋转域
      • ¥15 echarts中dataZoom报错
      • ¥15 求解答:《前端综合基础》作为一门课程的话,应该包含哪些内容?
      • ¥15 软件原型系统开发+实例测试