Postgres找到名字表或

时间:2014-12-25 14:55:31

标签: postgresql

enter image description here

如果有人是id_pers的客户或供应商或员工,我该如何找到。 我需要查询返回名称表或相对的东西。 我尝试了这个查询:

 SELECT p.id_pers, p.name_pers 
,c.id_customer as customer
,e.id_emp as employee
,s.id_supplier as supplier
      from person p
 left join employee e  on p.id_pers=e.id_pers
 left join customer c  on p.id_pers=c.id_pers 
 left join supplier s  on p.id_pers=s.id_pers 
    where id_pers='12'

我不知道如何获得人的本性。

1 个答案:

答案 0 :(得分:2)

可能有更优雅的方式来做,但这应该为你做

SELECT id_pers,
   CASE WHEN id_pers IN (SELECT id_supplier FROM supplier) THEN 'supplier'
        WHEN id_pers IN (SELECT id_customer FROM customer) THEN 'customer'
        WHEN id_pers IN (SELECT id_emp FROM employee) THEN 'employee'
   END
FROM person
WHERE id_pers=12;


id_pers |   case
--------+----------
     12 | supplier
(1 row)