SQL--子查詢
1、單行子查詢
select ename,deptno,sal
from emp
where deptno=(select deptno from dept where loc='NEW YORK');
2、多行子查詢
SELECT ename,job,sal
FROM EMP
WHERE deptno in ( SELECT deptno FROM dept WHERE dname LIKE 'A%');
3、多列子查詢
SELECT deptno,ename,job,sal
FROM EMP
WHERE (deptno,sal) IN (SELECT deptno,MAX(sal) FROM EMP GROUP BY deptno);
4、內聯檢視子查詢
(1)SELECT ename,job,sal,rownum
FROM (SELECT ename,job,sal FROM EMP ORDER BY sal);
(2)SELECT ename,job,sal,rownum
FROM ( SELECT ename,job,sal FROM EMP ORDER BY sal)
WHERE rownum<=5;
5、在HAVING子句中使用子查詢
SELECT deptno,job,AVG(sal) FROM EMP GROUP BY deptno,job HAVING AVG(sal)>(SELECT sal FROM EMP WHERE ename='MARTIN');
子查詢優化:
表結構
DROP TABLE IF EXISTS `job`;
CREATE TABLE `job` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of job
-- ----------------------------
INSERT INTO `job` VALUES ('1', 'Java', '2019-03-18 23:08:44');
INSERT INTO `job` VALUES ('2', 'Python', '2019-03-18 23:08:55');
INSERT INTO `job` VALUES ('3', 'C++', '2019-03-18 23:09:02');
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', '張三', '222');
INSERT INTO `user` VALUES ('2', '李四', '123');
INSERT INTO `user` VALUES ('3', '王五', '123');
-- ----------------------------
-- Table structure for `user_job`
-- ----------------------------
DROP TABLE IF EXISTS `user_job`;
CREATE TABLE `user_job` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`job_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user_job
-- ----------------------------
INSERT INTO `user_job` VALUES ('1', '1', '1');
INSERT INTO `user_job` VALUES ('2', '1', '2');
INSERT INTO `user_job` VALUES ('3', '2', '1');
INSERT INTO `user_job` VALUES ('4', '3', '1');
EXPLAIN
SELECT j.*
FROM job AS j
WHERE j.id IN (
SELECT job_id
FROM
user_job
WHERE user_id=1
)
EXPLAIN
SELECT j.*
FROM job AS j
INNER JOIN user_job AS uj ON j.id=uj.job_id
WHere uj.user_id=1
相關文章
- SQL--查詢SQL
- 查詢堵塞程式的幾種SQL--SQL
- 子查詢-表子查詢
- 複雜查詢—子查詢
- SQL查詢的:子查詢和多表查詢SQL
- 相關子查詢&非相關子查詢概念
- MySQL子查詢MySql
- 子串查詢
- 使用子查詢
- MYsql 子查詢MySql
- oracle子查詢Oracle
- 查詢子串
- 子查詢分解
- 11子查詢
- sql子查詢SQL
- informix子查詢ORM
- MySQL聯結查詢和子查詢MySql
- select查詢之三:子查詢
- mysql-分組查詢-子查詢-連線查詢-組合查詢MySql
- 區分關聯子查詢和非關聯子查詢
- sql語法相關子查詢與非相關子查詢SQL
- exist-in和關聯子查詢-非關聯子查詢
- MySQL之連線查詢和子查詢MySql
- 巢狀子查詢巢狀
- GORM subquery 子查詢GoORM
- Oracle with重用子查詢Oracle
- oracle with 子查詢用法Oracle
- select子查詢
- mysql的子查詢MySql
- Javaweb-子查詢JavaWeb
- MYSQL學習筆記25: 多表查詢(子查詢)[標量子查詢,列子查詢]MySql筆記
- Oracle查詢轉換(五)子查詢展開Oracle
- 關聯查詢子查詢效率簡單比照
- 【MySQL】檢視&子查詢MySql
- MySQL 相關子查詢MySql
- 教你使用SQLite 子查詢SQLite
- [MYSQL -14]使用子查詢MySql
- 【MySQL】子查詢之一MySql