SELECT * FROM city WHERE EXISTS (SELECT Name FROM country);
如下图所示:
4
由第三步可知,与EXISTS相对的是NOT EXISTS,当内层查询返回的值为false时 , 外层查询语句将进行查询,如果返回true时,将不进行查询或查询结果为空,代码如下:
SELECT * FROM city WHERE NOT EXISTS (SELECT Name FROM country);
如下图所示:
5
满足其中的任一条件,就可以通过该条件来执行外层查询语句 , 用关键字ANY,代码如下:
SELECT * FROM city WHERE Population = ANY (SELECT Population FROM country);
如下图所示:
6
子查询还包含比较运算符,有“="、“="、"!="等关键字,代码如下:
SELECT * FROM city WHERE Population =(SELECT Population FROM country WHERE Name = 'Afghanistan');
【mysql怎么写子查询 mysql 子查询】SELECT * FROM city WHERE Population =(SELECT Population FROM country WHERE Name = 'Afghanistan');
SELECT * FROM city WHERE Population !=(SELECT Population FROM country WHERE Name = 'Afghanistan');
如下图所示:
7
满足所有条件,只有满足内层查询语句返回所有结果,代码如下:
SELECT * FROM city WHERE Population = ALL (SELECT Population FROM country WHERE Name = 'Afghanistan');
如下图所示:
Mysql 子查询-- 查询分类名称为手机数码mysql怎么写子查询的所有商品
1.查询分类名为手机数码mysql怎么写子查询的ID
SELECT cid FROM category WHERE cname='手机数码';
2.得出ID为1mysql怎么写子查询的结果
SELECT * FROM product WHERE cno = (SELECT cid FROM category WHERE cname='手机数码');
子查询练习:
子查询练习及答案:
CREATE TABLE emp(
empno INT,
ename VARCHAR(50),
job VARCHAR(50),
mgr INT,
hiredate DATE,
sal DECIMAL(7,2),
comm DECIMAL(7,2),
deptno INT
) ;
INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO emp VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO emp VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO emp VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO emp VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO emp VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO emp VALUES(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);
INSERT INTO emp VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO emp VALUES(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);
INSERT INTO emp VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO emp VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO emp VALUES(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);
INSERT INTO emp VALUES(7981,'MILLER','CLERK',7788,'1992-01-23',2600,500,20);
CREATE TABLE dept(
deptnoINT,
dnameVARCHAR(14),
locVARCHAR(13)
);
INSERT INTO dept VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO dept VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO dept VALUES(40, 'OPERATIONS', 'BOSTON');
--最高工资
SELECT MAX(sal) FROM emp;
--最少工资
SELECT MIN(sal) FROM emp;
--最高工资的员工信息
SELECT * FROM emp WHERE sal = (SELECT MAX(sal) FROM emp);
--最低工资的员工信息
SELECT * FROM emp WHERE sal = (SELECT MIN(sal) FROM emp);
-- 单行子查询(= = = )
-- 查询出高于10号部门的平均工资的员工信息
推荐阅读
- 网络游戏奇迹,网络游戏奇迹还能玩吗
- python计算机专业会学吗,学python能就业吗
- 曲靖代理记账网络平台,曲靖会计公司
- mysql选择当前时间,mysql默认当前时间字段
- 求出源码java代码 java源代码的基本格式
- 巧影手机怎么制作水印,巧影水印怎么去掉
- 海外服务器好用吗,海外服务器ip有哪些
- 对对碰java游戏代码 对对碰游戏规则
- 什么是css的层叠特性,什么是css的层叠性和继承性