如何在if语句中使用if语句编写select语句

时间:2020-11-12 03:47:58

标签: sql postgresql

我想在postgres中写一个select语句,如果条件在哪里,该如何放置

要在这种情况下创建报告,假设我们的表中有20个国家/地区,并且我们有三种类型的员工,其中有contact, payroll and permanent个是employee_category,我想获取除India之外的所有员工详细信息和USA(如果这两个国家/地区拥有工资单和永久雇员,则应与这两个国家的雇员联系)

类似的东西

SELECT
r.employee_id AS E_ID,
r.Joining_date AS J_DATE,
r.employee_type AS E_TYPE,
'NIL'::text AS REMARKS,
i.source as SOURCE,
i.user_orgname as USER_ORGNAME
from master_employee r
INNER JOIN users_master AS i ON r.user_id = i.loginid and  i.userstatus = 'Active' 
WHERE 
if(country="india","USA"){
employee_category!='contract'
}
 and
date(r.createddate) = date(now())
ORDER BY r.createddate ASC ;

2 个答案:

答案 0 :(得分:2)

使用case语句应该可以。

SELECT
r.employee_id AS E_ID,
r.Joining_date AS J_DATE,
r.employee_type AS E_TYPE,
'NIL'::text AS REMARKS,
i.source as SOURCE,
i.user_orgname as USER_ORGNAME
from master_employee r
INNER JOIN users_master AS i ON r.user_id = i.loginid and  i.userstatus = 'Active' 
WHERE employee_category <> case when country in ("india","USA") then 'contract' end
 and date(r.createddate) = date(now())
ORDER BY r.createddate ASC ;

答案 1 :(得分:1)

请使用基本的布尔逻辑:

where (country not in ('india', 'USA') or
       employee_category <> 'contract'
      ) and
      date(r.createddate) = date(now())