如何通过连接两个在 oracle sql 中没有公共列的表来创建过程?
How to create a procedure by joining two tables that doesn't have a common column in oracle sql?
Table 1: EMP
列:EMPNO、ENAME、SAL、JOB、DEPTNO
Table 2 : 萨尔格莱德
列:GRADE、HISAL、LOSAL
给定员工的 EMPNO 作为程序的输入,我必须获得相应的 Grade 和 SAL 作为输出。
在 sal
加入 losal
和 hisal
之间。类似于:
SELECT g.grade,
e.sal
FROM emp e
LEFT JOIN salgrade g
ON g.losal <= e.sal
AND g.hisal >= e.sal
WHERE e.empno = ?;
或者您可以像下面这样考虑数据库设计:
create table EMP
(
EMPNO NUMBER(5),
ENAME VARCHAR2(50),
SAL NUMBER(4),
JOB VARCHAR2(20),
DEPTNO NUMBER(2),
GRADE AS (CASE WHEN SAL >= 1000 AND SAL < 2000 THEN 'LOW'
WHEN SAL >= 2000 AND SAL < 3000 THEN 'MEDIUM'
WHEN SAL >= 3000 AND SAL < 4000 THEN 'HIGH'
ELSE
'UNKNOWN'
END),
CONSTRAINT emp_pk PRIMARY KEY (EMPNO)
);
insert into emp (EMPNO,ENAME,SAL,JOB,DEPTNO) values (10001, 'Ram', 1000, 'King', 10 );
insert into emp (EMPNO,ENAME,SAL,JOB,DEPTNO) values (10002, 'Laxman', 2000, 'Minister', 20 );
insert into emp (EMPNO,ENAME,SAL,JOB,DEPTNO) values (10003, 'Sita', 3000, 'Queen', 30 );
insert into emp (EMPNO,ENAME,SAL,JOB,DEPTNO) values (10004, 'Ravan', 5000, 'Rakshas', 40 );
SELECT * FROM EMP;
Table 1: EMP
列:EMPNO、ENAME、SAL、JOB、DEPTNO
Table 2 : 萨尔格莱德
列:GRADE、HISAL、LOSAL
给定员工的 EMPNO 作为程序的输入,我必须获得相应的 Grade 和 SAL 作为输出。
在 sal
加入 losal
和 hisal
之间。类似于:
SELECT g.grade,
e.sal
FROM emp e
LEFT JOIN salgrade g
ON g.losal <= e.sal
AND g.hisal >= e.sal
WHERE e.empno = ?;
或者您可以像下面这样考虑数据库设计:
create table EMP
(
EMPNO NUMBER(5),
ENAME VARCHAR2(50),
SAL NUMBER(4),
JOB VARCHAR2(20),
DEPTNO NUMBER(2),
GRADE AS (CASE WHEN SAL >= 1000 AND SAL < 2000 THEN 'LOW'
WHEN SAL >= 2000 AND SAL < 3000 THEN 'MEDIUM'
WHEN SAL >= 3000 AND SAL < 4000 THEN 'HIGH'
ELSE
'UNKNOWN'
END),
CONSTRAINT emp_pk PRIMARY KEY (EMPNO)
);
insert into emp (EMPNO,ENAME,SAL,JOB,DEPTNO) values (10001, 'Ram', 1000, 'King', 10 );
insert into emp (EMPNO,ENAME,SAL,JOB,DEPTNO) values (10002, 'Laxman', 2000, 'Minister', 20 );
insert into emp (EMPNO,ENAME,SAL,JOB,DEPTNO) values (10003, 'Sita', 3000, 'Queen', 30 );
insert into emp (EMPNO,ENAME,SAL,JOB,DEPTNO) values (10004, 'Ravan', 5000, 'Rakshas', 40 );
SELECT * FROM EMP;