gradeid | startrange | endrange
----------------------
A | 50000 | 1000000
B | 25000 | 49999
C | 10000 | 24999
Employee
id | salary
-----------
1 | 23000
2 | 20000
3 | 100000
我想让所有员工在单一查询中获得成绩。由于没有公共字段,我可以使用两个子查询来完成此操作。
select id from employee where salary
between (select startrange from grade where gradeid='A')
and (select endrange from grade where gradeid='A')
还有其他更好的SQL查询吗?
答案 0 :(得分:1)
您可以使用JOIN
:
CREATE TABLE #grades(gradeid VARCHAR(100), startrange INT, endrange INT);
INSERT INTO #grades
VALUES ('A', 50000, 1000000),('B', 25000, 49999),('C', 10000, 24999);
CREATE TABLE #Employee(id INT, salary INT);
INSERT INTO #Employee VALUES(1, 23000),(2, 20000),(3, 100000);
SELECT e.*
FROM #Employee e
JOIN #grades g
ON e.salary BETWEEN startrange AND endrange
WHERE g.gradeid = 'A';
的 LiveDemo
强>