简体   繁体   English

where 子句中的表列值

[英]Table Column value in where clause

I have two table我有两张桌子

Employee员工

EmployeeId员工ID Name姓名 Job工作
123 123 David大卫 yes是的
124 124 Timothy提摩太 yes是的
125 125 Maxi马克西 No
126 126 Abie阿比

EmployeeResidence员工宿舍

Id ID EmployeeId员工ID EmployeeSpclField EmployeeSpclField EmployeeSpclValue EmployeeSpclValue
1 1 123 123 Married已婚 yes是的
2 2 123 123 Nationality国籍 MEX墨西哥
3 3 123 123 Job工作 Fitter钳工
4 4 124 124 Married已婚 yes是的
5 5 124 124 Nationality国籍 ARG ARG
6 6 125 125 Job工作 Driver司机

I need a Sql query that returns 1 or 0 based on the Bind value Employee id and I need the following conditions to be validated我需要一个基于 Bind 值 Employee id 返回 1 或 0 的 Sql 查询,我需要验证以下条件

  1. job should not be null 2)Nationality should not be null and he should belong to MEX工作不应为空 2)国籍不应为空且他应属于 MEX

So how can i do that ?那我该怎么做呢? I am building something like this我正在建造这样的东西

select 1 from Employee emp ,EmployeeResidence er 
where 
employeeid=:empid and emp.job is not null and 

I am struggling to include EmployeeSpclField column value to check if it is not null and belongs to MEX我正在努力包含 EmployeeSpclField 列值来检查它是否不为 null 并且属于 MEX

I might avoid a join and instead use exists logic here:我可能会避免加入,而是在此处使用存在逻辑:

SELECT e.*
FROM Employee e
WHERE Job IS NOT NULL AND
      EXISTS (SELECT 1 FROM EmployeeResidence er
              WHERE er.EmployeeId = e.EmployeeId AND
                    er.EmployeeSpclField = 'Nationality' AND
                    er.EmployeeSpclValue = 'MEX');

Select a 0/1 flag选择一个 0/​​1 标志

select case when exists (
    select 1 
    from Employee emp 
    join EmployeeResidence er 
      on emp.employeeid = er.employeeid and er.EmployeeSpclField = 'Nationality' and er.EmployeeSpclValue = 'MEX'
    where emp.employeeid=:empid and emp.job is not null ) then 1
   else 0 end flag 
from dual;

Try this:尝试这个:

Select
e.EmployeeID,
case when er.EmployeeID is null then 0 else 1 end as return_val
from
Employee e
left outer join
(select EmployeeId,
 max(case when EmployeeSpclField='Married' then EmployeeSpclValue else NULL end) as married,
 max(case when EmployeeSpclField='Nationality' then EmployeeSpclValue else NULL end) as Nationality,
 max(case when EmployeeSpclField='Job' then EmployeeSpclValue else NULL end) as Job
from 
 EmployeeResidence
group by
EmployeeId) er
on e.EmployeeId=er.EmployeeId
and e.Job is not null 
and (er.Nationality is not null and er.Nationality ='MEX')

One option might be using conditional aggregation in order to count desired conditions一种选择可能是使用条件聚合来计算所需的条件

SELECT SIGN(NVL(SUM(CASE WHEN er.EmployeeSpclField = 'Nationality' 
                          AND er.EmployeeSpclValue = 'MEX' 
                          AND e.Job IS NOT NULL THEN 1 ELSE 0 END),0)) 
    AS Result
  FROM Employee e
  JOIN EmployeeResidence er
    ON er.EmployeeId = e.EmployeeId
 WHERE e.EmployeeId = :empid

where SIGN() function is used considering the possibility to have duplicate records for Nationality value for EmployeeSpclField column per any of EmployeeId column's value.其中SIGN()函数是用来考虑可能有重复的记录Nationality的价值EmployeeSpclField每任何列EmployeeId列的值。

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM