CREATE TABLE grades AS SELECT to_char(s.grade) AS grade, s.losal AS startval, s.hisal AS endval FROM scott.salgrade s; CREATE TABLE customers AS SELECT e.empno AS cutsno, e.ename AS custname, to_char(e.deptno) AS custaddress, e.sal AS cust_credit_limit FROM scott.emp e;
SQL> SELECT custname, cust_credit_limit, grade 2 FROM customers, grades 3 WHERE (SELECT MAX(cust_credit_limit) FROM customers) BETWEEN startval AND 4 endval; CUSTNAME CUST_CREDIT_LIMIT GRADE ---------- ----------------- ---------------------------------------- SMITH 800.00 5 ALLEN 1600.00 5 WARD 1250.00 5 JONES 2975.00 5 MARTIN 1250.00 5 BLAKE 2850.00 5 CLARK 2450.00 5 SCOTT 3000.00 5 KING 5000.00 5 TURNER 1500.00 5 ADAMS 1100.00 5 JAMES 950.00 5 FORD 3000.00 5 MILLER 1300.00 5 14 rows selected
B 与 C 均有关联条件,限制条件分别限制customers与 grades ,产生的效果一样。
B
SQL> SELECT custname, grade 2 FROM customers, grades 3 WHERE (SELECT MAX(cust_credit_limit) FROM customers) BETWEEN startval AND 4 endval 5 AND cust_credit_limit BETWEEN startval AND endval; CUSTNAME GRADE ---------- ---------------------------------------- KING 5 1 row selected
SQL> SELECT custname, grade 2 FROM customers, grades 3 WHERE cust_credit_limit = (SELECT MAX(cust_credit_limit) FROM customers) 4 AND cust_credit_limit BETWEEN startval AND endval; CUSTNAME GRADE ---------- ---------------------------------------- KING 5 1 row selected
SQL> SELECT custname, grade 2 FROM customers, grades 3 WHERE cust_credit_limit IN (SELECT MAX(cust_credit_limit) FROM customers) 4 AND MAX(cust_credit_limit) BETWEEN startval AND endval; SELECT custname, grade FROM customers, grades WHERE cust_credit_limit IN (SELECT MAX(cust_credit_limit) FROM customers) AND MAX(cust_credit_limit) BETWEEN startval AND endval ORA-00934: group function is not allowed here