简体   繁体   English

SQL查询在Oracle DB中联接两个没有公共列的表

[英]SQL query to join two tables without common column in oracle DB

I have 2 tables employee and job_role as below. 我有2个表employeejob_role ,如下所示。 I need to write a SQL query to find designation of each employee by joining this table. 我需要编写一个SQL查询以通过加入此表来查找每个雇员的指定。

Input Table 输入表

Employee 雇员

e_id    e_name  Salary
-----------------------
1       ABC     1000
2       CDE     2000
3       GHI     3500
4       JKL     5000
5       MNO     4000
6       XYZ     3000

Job_role 职业角色

Designation   Sal_min   Sal_max
-------------------------------
Associate      1000      2000
Lead           2001      3000
Manager        3001      5000

Problem: if salary from employee table is in the range sal_min to sal_max , find the designation 问题:如果employee表中的薪水在sal_minsal_max范围内,请找到指定

Desired output: 所需的输出:

e_id    e_name  Salary  Designation
-----------------------------------    
 1      ABC     1000    Associate
 2      CDE     2000    Associate
 3      GHI     3500    Manager
 4      JKL     5000    Manager
 5      MNO     4000    Manager
 6      XYZ     3000    Lead

The ON clause can consist of other operations than = . ON子句可以包含=以外的其他运算。 Here you can use <= and >= (or BETWEEN if you like). 在这里,您可以使用<=>= (如果需要,可以使用BETWEEN )。

SELECT E.E_ID,
       E.E_NAME,
       JR.DESIGNATION
       FROM EMPLOYEE E
            LEFT JOIN JOB_ROLE JR
                      ON JR.SAL_MIN <= E.SALARY
                         AND JR.SAL_MAX >= E.SALARY;

(Note: I used LEFT JOIN for the case that there are any employees where the salary doesn't match. I guessed you rather want to see them with a NULL designation than not at all.) (注意:我使用LEFT JOIN的情况是,有任何员工的薪水都不匹配。我猜想您是希望看到他们的名字为NULL不是根本不喜欢。)

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

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