子查询:

嵌套在其他sql语句中的查询语句

例: select * from table where cole1= (select cole2 from table2);

子查询嵌套在查询内部,且必须出现在圆括号内(子查询结果必须是另一个查询的过滤条件)

 使用比较运算符的子查询: = ,>, <, !=

any 或 some 关键词:(只要价格大于查询价格的最小值就会显示)

例:查询书价大于图书类别编号为4的任意图书价格的所有图书信息

select * from bookinfo where prince > any (select price from bookinfo where book_category_id = 4);

或:

select * from bookinfo where prince > some (select price from bookinfo where book_category_id = 4);

ALL 关键词(只要价格大于查询价格的最大值才会显示)

select * from bookinfo where prince > all (select price from bookinfo where book_category_id = 4);

使用[NOT]IN的子查询

例:查询图书信息不是“医学”的全部信息

select * from bookinfo where book_category_id not in (select category_id from bookinfo where book= "医学" );

使用[not]exists子查询

select * from table where exists(子查询);

判断子查询是否返回行,如果返回,那么exists则返回ture,否则返回fales;

 

posted on 2019-07-28 14:22  wzc27229  阅读(129)  评论(0编辑  收藏  举报