使用WHERE和AND

时间:2016-01-11 12:18:56

标签: sql oracle

我有这样的查询

SELECT EDR.DEPT_ID             AS DEPT_ID,
  EMP.EMPLOYEE_ID                AS EMPLOYEE_ID,
  ELD.LOAN_ID                  AS LOAN_ID,
  ELD.LOAN_STATUS                       AS LOAN_STATUS
FROM EMP_DEPT_REL EDR
INNER JOIN EMPLOYEE_MANUAL EMP
ON EDR.EMPLOYEE_ID = EMP.EMPLOYEE_ID
LEFT OUTER JOIN EMPLOYEE_LOAN_DETAIL ELD
ON ELD.LOAN_ID   = EMP.LOAN_ID
AND ELD.DISPLAY_FLAG       = 'Y'
WHERE EDR.DEPT_ID IN ('123');
  1. Employee_Manual是我们将拥有由应用程序手动输入的员工详细信息的表。
  2. Emp_dept_rel是员工与部门之间的关系。
  3. Employee_loan_detail将获得组织中所有员工的贷款详细信息。
  4. 我想从部门中关联的所有员工获取数据,我将其作为输入传递。

    我需要的是,

    • 获取employee_manual表中的所有员工。
    • 如果有的话 是Employee_loan_details中的条目,然后我应该得到详细信息 贷款,只有当国旗设置为" Y"在Employee_loan_details中。
    • 即使没有详细信息,我们也需要显示员工 Employee_loan_details

    需要获取在特定部门贷款的员工的数据。如果员工有外部贷款,我们需要显示贷款明细。(DISPLAY_FLAG =' Y')如果是组织提供的内部贷款,它将在Employee_loan_detail中,并标记为DISPLAY_FLAG =" N"

    EMPLOYEE_LOAN_DETAIL
    <table>
    <tr><th>EMPLOYEE_ID</th><th>LOAD_ID</th><th>DISPLAY_FLAG</th><tr>
    <tr><td>123</td><td>50</td><td>Y</td></tr>
    <tr><td>456</td><td>51</td><td>N</td></tr>
    <table>
    
    emp_dept_rel 
    <table>
    <tr><th>EMP_ID</th><th>DEPT_ID</th><tr>
    <tr><td>123</td><td>987</td></tr>
    <tr><td>456</td><td>987</td></tr>
    <tr><td>789</td><td>987</td></tr>
    <table>
    
    employee_manual 
    <table>
    <tr><th>EMPLOYEE_ID</th><th>LOAD_ID</th><th>DEPT_ID</th><tr>
    <tr><td>123</td><td>50</td><td>987</td></tr>
    <tr><td>456</td><td>51</td><td>987</td></tr>
    <tr><td>789</td><td></td><td>987</td></tr>
    <table>
    

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT
  EDR.DEPT_ID             AS   DEPT_ID,
  EMP.EMPLOYEE_ID         AS   EMPLOYEE_ID,
  ELD.LOAN_ID             AS   LOAN_ID,
  ELD.LOAN_STATUS         AS   LOAN_STATUS

FROM        EMPLOYEE_MANUAL EMP

INNER JOIN  EMP_DEPT_REL EDR
            ON EDR.EMPLOYEE_ID = EMP.EMPLOYEE_ID

LEFT OUTER JOIN EMPLOYEE_LOAN_DETAIL ELD  
            ON ELD.LOAN_ID   = EMP.LOAN_ID

WHERE EDR.DEPT_ID IN ('123') AND ELD.DISPLAY_FLAG       = 'Y';