设为首页 收藏本站
查看: 321|回复: 0

[经验分享] MySQL多表查询之子查询

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-1-20 08:52:39 | 显示全部楼层 |阅读模式
ANY和SOME子查询
Any和some关键字是同义词,表示满足其中任一条件。
通过创建表达式,对返回的结果进行比较,并输出符合条件的结果。

mysql> select * from t2;
+------+-------+------+
| id  | name  | age  |
+------+-------+------+
|   1 | Mark  |   29 |
|   2 | Frank |   32 |
|   3 | Niko  |   27 |
+------+-------+------+
3 rows in set (0.00 sec)

mysql> select * from t1;
+------+-------+------+
| id  | name  | age  |
+------+-------+------+
|   1 | Tube  |   31 |
|   2 | Kevin |   34 |
|   3 | Todd  |   32 |
+------+-------+------+
3 rows in set (0.00 sec)


mysql> select * from t2 where age >any (select age from t1);
+------+-------+------+
| id  | name  | age  |
+------+-------+------+
|   2 | Frank |   32 |
+------+-------+------+
1 row in set (0.07 sec)

EXITS和NOT EXITS子查询
EXITS和NOT EXITS可以说是一种判断子查询。
系统对子查询进行运算,并判断是否有返回行。如果有则执行外层语句中的查询,如果没有则不进行查询。

mysql> select * from log;
+------+----------+------------------------------------------------+
| id  | category | log                                            |
+------+----------+------------------------------------------------+
|   1 | Nginx    | upstream timed out(110: Connection timed out) |
|   2 | Apache   | File does notexist:/var/www/html/1.html       |
|   3 | Apache   | Could not openfile:/var/images/banner.gif     |
|   4 | MySQL    | InnoDB: Unable tolock ./ibdata1, error: 11    |
+------+----------+------------------------------------------------+
4 rows in set (0.05 sec)

mysql>
mysql>
mysql> select * from state;
+---------+-------+---------+---------+
| apache | Nginx | App     | MySQL   |
+---------+-------+---------+---------+
| Succeed | Fail  | Succeed | Succeed |
+---------+-------+---------+---------+
1 row in set (0.09 sec)

mysql> select * from log where category= 'Nginx' and exists (select * from state where Nginx='Fail');
+------+----------+------------------------------------------------+
| id  | category | log                                            |
+------+----------+------------------------------------------------+
|   1 | Nginx    | upstream timed out(110: Connection timed out) |
+------+----------+------------------------------------------------+
1 row in set (0.00 sec)

如果查询结果为真,显示;为假不显示。

mysql> select * from log where category= 'Apache';
+------+----------+--------------------------------------------+
| id  | category | log                                        |
+------+----------+--------------------------------------------+
|   2 | Apache   | File does notexist:/var/www/html/1.html   |
|   3 | Apache   | Could not openfile:/var/images/banner.gif |
+------+----------+--------------------------------------------+
2 rows in set (0.00 sec)

mysql> select * from log where category= 'Apache' and exists (select * from state where apache='Fail');
Empty set (0.00 sec)

IN子查询

IN关键字对子查询结果进行运算,并判断是否返回行。如果有则执行外部查询语句。如果没有则不执行。
外查询返回的结果匹配子查询的结果。
mysql> select * from People;
+------+------+-----------+-------------+
| name | age  | certnum  | phone       |
+------+------+-----------+-------------+
| Lee |   20 | 123456789 | 13021981234 |
| John |  32 | 987654321 | 13998763456 |
| Lucy |  45 | 678954321 | 13098784321 |
| Ken |   33 | 986745321 | 18609073544 |
+------+------+-----------+-------------+
4 rows in set (0.03 sec)

mysql> select * from Blacklist;
+------+------+-------+--------+
| ID  | Name | Price | Level  |
+------+------+-------+--------+
|   1 | John |  3000 | normal |
|   2 | Lucy | 24000 | hard   |
+------+------+-------+--------+
2 rows in set (0.08 sec)

mysql> select * from People where nameIN
   -> (select Name from Blacklist);
+------+------+-----------+-------------+
| name | age  | certnum  | phone       |
+------+------+-----------+-------------+
| John |  32 | 987654321 | 13998763456 |
| Lucy |  45 | 678954321 | 13098784321 |
+------+------+-----------+-------------+
2 rows in set (0.00 sec)

IN子查询适合从大的结果集拾取指定条件的小的结果集。这个小的结果集同时也是另外一个表中的内容。

Union子查询 (唯一性)
Union用于合并查询结果。可以将多条select语句查询的结果组合成单个结果集。
列数必须相同,使用ALL关键字则拼接两张表。

mysql> select * from People;
+------+------+-----------+-------------+
| name | age  | certnum  | phone       |
+------+------+-----------+-------------+
| Lee |   20 | 123456789 | 13021981234 |
| John |  32 | 987654321 | 13998763456 |
| Lucy |  45 | 678954321 | 13098784321 |
| Ken |   33 | 986745321 | 18609073544 |
+------+------+-----------+-------------+
4 rows in set (0.00 sec)

mysql> create table People2 like People;
Query OK, 0 rows affected (0.04 sec)

mysql> insert into People2 select * from People where age=45;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0  Warnings: 0

mysql> select * from People2;
+------+------+-----------+-------------+
| name | age  | certnum  | phone       |
+------+------+-----------+-------------+
| Lucy |  45 | 678954321 | 13098784321 |
+------+------+-----------+-------------+
1 row in set (0.00 sec)

mysql> insert into People2 values('jingjing',31,123321456,18812344321);
Query OK, 1 row affected (0.01 sec)

mysql> select * from People;
+------+------+-----------+-------------+
| name | age  | certnum  | phone       |
+------+------+-----------+-------------+
| lee |   20 | 123456789 | 13021981234 |
| John |  32 | 987654321 | 13998763456 |
| Lucy |  45 | 678954321 | 13098784321 |
| Ken |   33 | 986745321 | 18609073544 |
+------+------+-----------+-------------+
4 rows in set (0.00 sec)

mysql> select * from People2;
+----------+------+-----------+-------------+
| name    | age  | certnum   | phone      |
+----------+------+-----------+-------------+
| Lucy    |   45 | 678954321 | 13098784321 |
| jingjing |   31 | 123321456 | 18812344321 |
+----------+------+-----------+-------------+
2 rows in set (0.00 sec)


mysql> select * from People union select* from People2;
+----------+------+-----------+-------------+
| name    | age  | certnum   | phone      |
+----------+------+-----------+-------------+
| lee     |   20 | 123456789 | 13021981234 |
| John    |   32 | 987654321 | 13998763456 |
| Lucy    |   45 | 678954321 | 13098784321 |
| Ken     |   33 | 986745321 | 18609073544 |
| jingjing |   31 | 123321456 | 18812344321 |
+----------+------+-----------+-------------+
5 rows in set (0.00 sec)

重复的一条数据不存在了。


mysql> select * from People union allselect * from People2;
+----------+------+-----------+-------------+
| name    | age  | certnum   | phone      |
+----------+------+-----------+-------------+
| lee     |   20 | 123456789 | 13021981234 |
| John    |   32 | 987654321 | 13998763456 |
| Lucy    |   45 | 678954321 | 13098784321 |
| Ken     |   33 | 986745321 | 18609073544 |
| Lucy    |   45 | 678954321 | 13098784321 |
| jingjing |   31 | 123321456 | 18812344321 |
+----------+------+-----------+-------------+
6 rows in set (0.00 sec)

简单的拼接

mysql> select * from Blacklist;
+------+------+-------+--------+
| ID  | Name | Price | Level  |
+------+------+-------+--------+
|   1 | John |  3000 | normal |
|   2 | Lucy | 24000 | hard   |
+------+------+-------+--------+
2 rows in set (0.00 sec)

mysql> select * from People2;
+----------+------+-----------+-------------+
| name    | age  | certnum   | phone      |
+----------+------+-----------+-------------+
| Lucy    |   45 | 678954321 | 13098784321 |
| jingjing |   31 | 123321456 | 18812344321 |
+----------+------+-----------+-------------+
2 rows in set (0.00 sec)



当列数据类型不一样的时候

mysql> select * from Blacklist unionselect * from People2;
+----------+------+-----------+-------------+
| ID      | Name | Price     | Level       |
+----------+------+-----------+-------------+
| 1       | John |      3000 | normal      |
| 2       | Lucy |     24000 | hard        |
| Lucy    | 45   | 678954321 | 13098784321 |
| jingjing | 31   | 123321456 | 18812344321 |
+----------+------+-----------+-------------+
4 rows in set (0.00 sec)

mysql> select * from Blacklist union allselect * from People2;
+----------+------+-----------+-------------+
| ID      | Name | Price     | Level       |
+----------+------+-----------+-------------+
| 1       | John |      3000 | normal      |
| 2       | Lucy |     24000 | hard        |
| Lucy    | 45   | 678954321 | 13098784321 |
| jingjing | 31   | 123321456 | 18812344321 |
+----------+------+-----------+-------------+
4 rows in set (0.00 sec)

加不加ALL一样



mysql> select name,price from Blacklistunion all select name,certnum from People2;
+----------+-----------+
| name    | price     |
+----------+-----------+
| John    |      3000 |
| Lucy    |     24000 |
| Lucy    | 678954321 |
| jingjing | 123321456 |
+----------+-----------+
4 rows in set (0.00 sec)

mysql> select name,price from Blacklistunion select name,certnum from People2;
+----------+-----------+
| name    | price     |
+----------+-----------+
| John    |      3000 |
| Lucy    |     24000 |
| Lucy    | 678954321 |
| jingjing | 123321456 |
+----------+-----------+
4 rows in set (0.00 sec)


mysql> update People2 set certnum=24000where name='Lucy';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1 Warnings: 0



mysql> select name,certnum from People2where name='Lucy';
+------+---------+
| name | certnum |
+------+---------+
| Lucy |  24000 |
+------+---------+
1 row in set (0.00 sec)


mysql> select name,price from Blacklistunion select name,certnum from People2;
+----------+-----------+
| name    | price     |
+----------+-----------+
| John    |      3000 |
| Lucy    |     24000 |
| jingjing | 123321456 |
+----------+-----------+
3 rows in set (0.00 sec)

mysql> select name,price from Blacklistunion all select name,certnum from People2;
+----------+-----------+
| name    | price     |
+----------+-----------+
| John    |      3000 |
| Lucy    |     24000 |
| Lucy    |     24000 |
| jingjing | 123321456 |
+----------+-----------+
4 rows in set (0.00 sec)


能不能去重:取决于列数据类型一样,数据一样。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-166799-1-1.html 上篇帖子: Centos7安装phpldapadmin 下篇帖子: MySQL多表查询
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表