这是我的salgrade表
grade losal hisal
1 700.00 1200.00
2 1201.00 1400.00
3 1401.00 2000.00
4 2001.00 3000.00
5 3001.00 9999.00
这是我的emp表
sal empno ename
800.00 7369 SMITH
1600.00 7499 ALLEN
1250.00 7521 WARD
2975.00 7566 JONES
1250.00 7654 MARTIN
2850.00 7698 BLAKE
2450.00 7782 CLARK
3000.00 7788 SCOTT
5000.00 7839 KING
1500.00 7844 TURNER
1100.00 7876 ADAMS
950.00 7900 JAMES
3000.00 7902 FORD
1300.00 7934 MILLER
通过使用这些表格,我需要在给出两个等级时选择sal
答案 0 :(得分:1)
您仍然可以JOIN
使用比较,这会影响插值,例如找到Sal Grade 1的所有员工:
SELECT emp.empno, emp.sal, salgrade.grade
FROM emp
INNER JOIN salgrade
ON emp.sal BETWEEN salgrade.losal and salgrade.hisal
WHERE salgrade.grade = 1;
(您可能希望确保salgrade
中的数据涵盖所有薪资范围,并且不会自相矛盾,例如删除salgrade
中的重复项
答案 1 :(得分:0)
使用存储过程:
CREATE PROCEDURE ProcedureName
-- Add the parameters for the stored procedure here
@grade int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Declare
@HiGrade decimal(9,2),
@LoGrade decimal(9,2)
select @HiGrade = a.hisal, @LoGrade = a.losal
from (select distinct grade, losal, hisal
from salgrade
where grade = @grade) a
select *
from Emp
Where sal between @LoGrade and @HiGrade
END
GO