MySQL高效编程学习笔记(四)--在查询的基础上进行数据检索

MySQL高效编程学习笔记(四)--在查询的基础上进行数据检索

在查询的SQL中嵌入的查询通常被称为子查询,可以在别的检索结果上进行新的检索。

1. 基本子查询

在where中嵌入子查询,比如要查询表history_balance_data中压力值balance_press1大于平均压力值的所有压力值及其对应id.

   select ID,balance_press1 from history_balance_data where balance_press1 > 
   (select avg(balance_press1) from history_balance_data);

2. 多个返回值的子查询

使用IN,NOT IN要求多个值运算符子查询的结果是可以返回多个值的,比如想要检索在2010-07-24那天没下单的用户:

select name,address from user where uid NOT IN (select uid from order_basic 
where odate = ‘2010-07-24’);

3. 子查询与EXISTS运算符

EXISTS运算符是对查询中抽出的记录做是否存在检查的运算符

select s.name,s.sex,g.name from student as s inner join goods as g on s.goodsID=g.ID where exists (select * from student where s.goodsID=g.ID );

猜你喜欢

转载自blog.csdn.net/HuYingJie_1995/article/details/82778507