我有一个问题。我有两个员工和工资表,我想显示所有员工的全名和工资等级,其grade_ID是2或3,按employee_ID排序。
以下是表格:
Salary_ID LOW_SALARY High Salary
1 1 250000
2 250001 500000
3 500001 750000
4 750001 999999
EMPLOYEE_ID FIRST_NAME Last_NAME Salary
1 James Smith 800000
22 Roy Red 375000
2 Ron Johnson 550000
5 Rob Green 225000
我知道我必须加入这些表,这是我到目前为止所做的:
SELECT employees2.FIRST_NAME,
employees2.last_name,
salary_grades.SALARY_GRADE_ID,
employees2.SALARY
FROM employees2,
salary_grades
ORDER BY employees2.EMPLOYEE_ID;
接下来我该怎么办?我被卡住了。谁能帮我吗?谢谢。
答案 0 :(得分:3)
这可能是您正在寻找的查询:
SELECT E.EMPLOYEE_ID
,E.FIRST_NAME
,E.LAST_NAME
,E.SALARY
FROM employees E
INNER JOIN salary S ON S.LOW_SALARY <= E.SALARY
AND S.HIGH_SALARY >= E.SALARY
AND S.SALARY_ID IN (2,3)
ORDER BY E.EMPLOYEE_ID
希望这会对你有所帮助。
答案 1 :(得分:0)
只需使用一个连接条件,将雇员工资与工资等级的上限和下限进行比较,即
SELECT E.*, S.* FROM Employee AS E
INNER JOIN dbo.Salary AS S
ON S.LOW_SALARY <= E.Salary AND S.High_Salary >= E.Salary
如果需要,您可以按Salary_Id
在2到3之间进行过滤
注意:此语法应适用于3种类型的服务器。
测试数据,对于SQL Server:
CREATE TABLE Salary (Salary_Id INT, LOW_SALARY INT, High_Salary INT)
INSERT INTO Salary VALUES(1,1,250000)
INSERT INTO Salary VALUES(2,250001,500000)
INSERT INTO Salary VALUES(3,500001,750000)
INSERT INTO Salary VALUES(4,750001,999999)
CREATE TABLE Employee (Employee_Id INT, FIRST_NAME varchar(50), Last_NAME varchar(50), Salary INT)
INSERT INTO Employee VALUES(1, 'James', 'Smith', 800000)
答案 2 :(得分:0)
我将给出SQL Server(2008+)答案。
SELECT E.FIRST_NAME, E.last_name, S.SALARY_GRADE_ID, E.SALARY
FROM employees2 E
JOIN salary_grades S ON (E.SALARY BETWEEN S.LOW_SALARY AND S.HIGH_SALARY) AND S.SALARY_ID IN (2, 3)
ORDER BY E.EMPLOYEE_ID;
答案 3 :(得分:0)
SELECT employees2.FIRST_NAME, employees2.LAST_NAME, employees2.SALARY, salary_grades.SALARY_GRADE_ID
FROM employees2, salary_grades
WHERE employees2.SALARY BETWEEN salary_grades.LOW_SALARY AND salary_grades.HIGH_SALARY
ORDER BY salary_grades.SALARY_GRADE_ID
所以我尝试使用这个脚本,它可以工作,但我使用IN在sqldeveloper中显示2和3的范围?