使用IF EXISTS连接SQL select语句

时间:2012-11-20 22:02:18

标签: sql oracle

道歉,如果这是一个愚蠢的问题,但我无法理解这一点。我想只选择带冒号的值(如果存在),该值为“county”。但是,我不确定我是否正确地采取了这种方式?任何帮助将不胜感激。

SELECT pe.personname AS "Person Name", pe.street||','||pe.town||','||
    IF NOT EXISTS(SELECT pe.county FROM persons pe WHERE pe.county = '')
    THEN pe.county||','|| 
    ELSE
    END IF
 pe.postcode as "Persons Address"
 FROM persons pe

更新版本,如果没有县,则会输出多个逗号。

SELECT personname as "Person Name"
street||','||town||
   CASE county
   WHEN '' THEN ''
   ELSE ','||county
   END
||','||postcode as "Person Address"
from Suppliers;

3 个答案:

答案 0 :(得分:1)

我已经为此而去了缺少关键字错误: -

 SELECT personname AS "Person Name", 
   street || ',' || town || ',' ||
     CASE WHEN county > ''
     THEN county || ','
     ELSE ''
     END 
   || postcode AS "Person Address"
 FROM Persons

答案 1 :(得分:0)

我认为你的意思是逗号。

SELECT pe.personname AS "Person Name", pe.street||','||pe.town||
  IF pe.county <> ''
  THEN ','||pe.county
  ELSE ''
  END IF
pe.postcode as "Persons Address"
FROM persons pe

它可能更干净,(你可能不得不)这样做:

SELECT pe.personname AS "Person Name",
  IF pe.county <> ''
  THEN pe.street||','||pe.town||','||pe.county
  ELSE pe.street||','||pe.town
  END IF
pe.postcode as "Persons Address"
FROM persons pe

答案 2 :(得分:0)

如果你想获得两列,一列是人名,第二列是地址部分,我会:

    select pe.personname, pe.street || ',' || pe.town || ',' || 
(case county when not null then (county || ',') else '' end), postcode

使用案例陈述来评估县,如果有一个用',''来'插入'',