合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
子查询,即子选择、嵌套查询、嵌套选择; 子查询在逻辑上一般是两个查询(内查询、外查询); 使用子查询的好处是,原本需要多个查询语句可以用一个查询完成,缺点是内查询不支持索引,所以一般会引起性能问题。 1、按照结果集,对子查询可以分为: ① 标量子查询 通常与比较运算符一起使用,返回的是一个具体值。 ② 行子查询 通常与 = 操作符一起使用,返回的是一行数据。 ③ 列子查询 通常与 in 操作符一起使用,返回的是一列数据。 ④ 表子查询 通常返回 M 到 N 行数据。 2、按照语句,对子查询可以分为: ① 用在 where 子句中的叫 where 型子查询 比如: select * from aa where id > (select max(id) from bb); select * from aa where id in (select id from bb); select * from aa where id,name in (select id,name from bb); ② 用在 from 子句中的叫 from 型子查询 比如: select * from (select * from bb where id > 10) as B where name like "%shang%"; ③ 用在 exists 子句中的叫 exists 型子查询 如果内查询为 true,那么外查询显示出来,比如: select * fom aa where exists(select * from bb where bb.id = aa.id); ps:使用字段时指定表名。