一.简单的嵌套查询
在SQL语言中,一个 SELECT-FROM-WHERE 语句称为一个查询块。将一个查询块嵌套在另一个查询块的 WHERE 子句或 HAVING 短语的条件中的查询称为 嵌套查询,例如:
select * from content_bidding_extension_wkf2
where uuid in
(select bidding_uuid from lbb_content_bidding_extension_2_project4);
二.多重嵌套查询
在嵌套循环的select语句中再添加一个或多个嵌套查询,这种查询就是多重嵌套查询,例如:
select * from content_bidding_extension_wkf2
where uuid in
(select bidding_uuid from lbb_content_bidding_extension_2_project4
where project_uuid in
(select uuid from project));
三.带有ANY(SOME)或ALL谓词,以及相关运算符的嵌套查询
运算符主要有>、<、=、>=、<=、!=、或<>等
四.相关易错点
1.在嵌套语句中使用where in和where =的区别:
如果子查询只有1条记录,where in和where =是没有区别,如果子查询有多条记录,用=会出错的,
错误,例如:
select * from lbb_content_bidding_extension_2
where uuid =
(select bidding_uuid from lbb_content_bidding_extension_2_project4
where project_uuid = (select uuid from project));
当查询有多种结果是,就会报错.如图:
,所以in是无论只查询有多少条记录都可以使用,而=只有当子查询只有1条记录的时候才能使用,所以嵌套查询最好用in,别用=