首页 > 数据库 >10:子查询-MySQL

10:子查询-MySQL

时间:2022-11-11 10:38:23浏览次数:38  
标签:10 money where +-------+-------+ 查询 eatery MySQL stuId select


目录

  • ​​10.1 子查询基本语法​​
  • ​​10.2 in 和 not in​​
  • ​​10.3 exists 和 not exists​​
  • ​​10.4 基础结束语​​

10.1 子查询基本语法

将一个查询的结果作为另一个查询的数据来源或判断条件

一般情况下子查询结果返回超过1行用​​in​​​,只有一行可以用​​=​

select * from stu where stuId  in (select stuId from eatery where money > 800);
select * from stu where stuId = (select stuId from eatery where money > 1000);
mysql> select * from eatery;
+----+----------+-------+
| id | money | stuId |
+----+----------+-------+
| 0 | 999.0000 | 5 |
| 1 | 20.5000 | NULL |
| 2 | 78.6000 | 4 |
| 3 | 99.9000 | NULL |
| 4 | 748.4000 | 4 |
| 5 | 748.4000 | NULL |
| 6 | 999.0000 | 5 |
| 7 | 345.0000 | 4 |
+----+----------+-------+
8 rows in set (0.00 sec)

mysql> select * from stu;
+-------+-------+
| stuId | name |
+-------+-------+
| 4 | frank |
| 5 | Tom |
+-------+-------+
2 rows in set (0.00 sec)


mysql> select stuId from eatery where money > 800;
+-------+
| stuId |
+-------+
| 5 |
| 5 |
+-------+
2 rows in set (0.00 sec)

mysql> select * from stu where stuId in (select stuId from eatery where money > 800);
+-------+------+
| stuId | name |
+-------+------+
| 5 | Tom |
+-------+------+
1 row in set (0.00 sec)

10.2 in 和 not in

mysql> select * from stu where stuId not in (select stuId from eatery where money > 800);
+-------+-------+
| stuId | name |
+-------+-------+
| 4 | frank |
+-------+-------+
1 row in set (0.00 sec)

10.3 exists 和 not exists

​exists​​是一个存在的条件,只要子查询存在,就把主查询所有的列出来

mysql> select stuId from eatery where money > 900;
+-------+
| stuId |
+-------+
| 5 |
| 5 |
+-------+
2 rows in set (0.00 sec)

mysql> select * from stu where exists (select stuId from eatery where money > 900);
+-------+-------+
| stuId | name |
+-------+-------+
| 4 | frank |
| 5 | Tom |
+-------+-------+
2 rows in set (0.00 sec)

10.4 基础结束语

多练习,现在如果你熟悉之前的课程,那么实习就基本达标了,后面的内容属于比较难的内容,对于实习生来说不是特别重要


标签:10,money,where,+-------+-------+,查询,eatery,MySQL,stuId,select
From: https://blog.51cto.com/u_15872973/5843037

相关文章

  • 11:高级部分-MySQL
    目录​​(一)view视图​​​​1.开场​​​​2.view视图创建、使用以及作用​​​​3.显示视图​​​​4.更新和删除视图​​​​5.视图算法:temptable,merge​​​​(......
  • centos8 yum安装docker 20.10.21 failed to load listeners: no sockets found via so
    vim/usr/lib/systemd/system/docker.serviceExecStart=/usr/bin/dockerd-Hunix://--containerd=/run/containerd/containerd.sock#ExecStart=/usr/bin/dockerd-Hfd......
  • 0:数据库的产生-MySQL
    目录​​0.1什么是数据库database​​​​0.2抛出问题,数据库的产生​​​​0.3数据库萌芽阶段的发展历程​​​​0.4CRUD​​​​0.5层次模型​​​​0.6网状模型​......
  • Prometheus 监控Mysql服务器及Grafana可视化
    Prometheus监控Mysql服务器及Grafana可视化、mysql_exporter:用于收集MySQL性能信息。使用版本mysqld_exporter0.11.0官方地址使用文档:https://github.com/promethe......
  • 「题解」Codeforces 1098D Eels
    暴力是,每次挑出最小的两个合并。需要观察到没有产生贡献的次数很小。考虑最小的那个数的大小,如果一次合并没有产生贡献,那么最小的数至少\(\times2\).所以最多会有\(\mat......
  • mysql常用操作
    查看表的字符集语法:showtablestatusfrom库名like表名; mysql8版本查看MYSQL数据库服务器和数据库字符集方法一:showvariableslike'%character%';方法二:showv......
  • MYSQL join..on 后的and 和where的区别
    今天在写SQL语句时发现一个问题selectcount(1)ascountfromsmbms_billbleftjoinsmbms_providerponb.providerId=p.id......
  • mybatis查询时报下标越界异常
    mybatis的sql语句没有问题,但是查询时报下标越界的异常:Cause:java.lang.IndexOutOfBoundsException:Index:3,Size:3很有可能是mybatis映射的实体类的无参构造写有写,......
  • [A202211110354]
    [A202211110354](2022,南开大学)设\(x_n=\displaystyle\sum_{k=0}^n{\frac{1}{k!}}\),\(n=1,2,\cdots\),求极限\[\lim_{n\rightarrow\infty}\left(\frac{\lnx_n}{\sqr......
  • [A202211110303]
    [A202211110303](2022,同济大学)已知\(\{a_n\}\)是一个实数列,\(0<|\lambda|<1\).证明:\(\displaystyle\lim_{n\rightarrow\infty}a_n=a\)的充要条件是\[\lim_{n\rig......