MySQL表中的连接方式其实非常简单,这里就简单的罗列出他们的特点。
表的连接(JOIN)可以分为内连接(JOIN/INNER JOIN)和外连接(LEFT JOIN/RIGHT JOIN)。
首先我们看一下我们本次演示的两个表:
mysql> SELECT * FROM student; +------+----------+------+------+ | s_id | s_name | age | c_id | +------+----------+------+------+ | 1 | xiaoming | 13 | 1 | | 2 | xiaohong | 41 | 4 | | 3 | xiaoxia | 22 | 3 | | 4 | xiaogang | 32 | 1 | | 5 | xiaoli | 41 | 2 | | 6 | wangwu | 13 | 2 | | 7 | lisi | 22 | 3 | | 8 | zhangsan | 11 | 9 | +------+----------+------+------+ 8 rows in set (0.00 sec) mysql> SELECT * FROM class; +------+---------+-------+ | c_id | c_name | count | +------+---------+-------+ | 1 | MATH | 65 | | 2 | CHINESE | 70 | | 3 | ENGLISH | 50 | | 4 | HISTORY | 30 | | 5 | BIOLOGY | 40 | +------+---------+-------+ 5 rows in set (0.00 sec)
首先,表要能连接的前提就是两个表中有相同的可以比较的列。
1.内连接
mysql> SELECT * FROM student INNER JOIN class ON student.c_id = class.c_id; +------+----------+------+------+------+---------+-------+ | s_id | s_name | age | c_id | c_id | c_name | count | +------+----------+------+------+------+---------+-------+ | 1 | xiaoming | 13 | 1 | 1 | MATH | 65 | | 2 | xiaohong | 41 | 4 | 4 | HISTORY | 30 | | 3 | xiaoxia | 22 | 3 | 3 | ENGLISH | 50 | | 4 | xiaogang | 32 | 1 | 1 | MATH | 65 | | 5 | xiaoli | 41 | 2 | 2 | CHINESE | 70 | | 6 | wangwu | 13 | 2 | 2 | CHINESE | 70 | | 7 | lisi | 22 | 3 | 3 | ENGLISH | 50 | +------+----------+------+------+------+---------+-------+ 7 rows in set (0.00 sec)
简单的讲,内连接就是把两个表中符合条件的行的所有数据一起展示出来,即如果不符合条件,即在表A中找得到但是在B中没有(或者相反)的数据不予以显示。
2.外连接
mysql> SELECT * FROM student LEFT JOIN class ON student.c_id = class.c_id; +------+----------+------+------+------+---------+-------+ | s_id | s_name | age | c_id | c_id | c_name | count | +------+----------+------+------+------+---------+-------+ | 1 | xiaoming | 13 | 1 | 1 | MATH | 65 | | 2 | xiaohong | 41 | 4 | 4 | HISTORY | 30 | | 3 | xiaoxia | 22 | 3 | 3 | ENGLISH | 50 | | 4 | xiaogang | 32 | 1 | 1 | MATH | 65 | | 5 | xiaoli | 41 | 2 | 2 | CHINESE | 70 | | 6 | wangwu | 13 | 2 | 2 | CHINESE | 70 | | 7 | lisi | 22 | 3 | 3 | ENGLISH | 50 | | 8 | zhangsan | 11 | 9 | NULL | NULL | NULL | +------+----------+------+------+------+---------+-------+ 8 rows in set (0.00 sec) mysql> SELECT * FROM student RIGHT JOIN class ON student.c_id = class.c_id; +------+----------+------+------+------+---------+-------+ | s_id | s_name | age | c_id | c_id | c_name | count | +------+----------+------+------+------+---------+-------+ | 1 | xiaoming | 13 | 1 | 1 | MATH | 65 | | 4 | xiaogang | 32 | 1 | 1 | MATH | 65 | | 5 | xiaoli | 41 | 2 | 2 | CHINESE | 70 | | 6 | wangwu | 13 | 2 | 2 | CHINESE | 70 | | 3 | xiaoxia | 22 | 3 | 3 | ENGLISH | 50 | | 7 | lisi | 22 | 3 | 3 | ENGLISH | 50 | | 2 | xiaohong | 41 | 4 | 4 | HISTORY | 30 | | NULL | NULL | NULL | NULL | 5 | BIOLOGY | 40 | +------+----------+------+------+------+---------+-------+ 8 rows in set (0.00 sec)
上面分别展示了外连接的两种情况:左连接和右连接。这两种几乎是一样的,唯一的区别就是左连接的主表是左边的表,右连接的主表是右边的表。而外连接与内连接不同的地方就是它会将主表的所有行都予以显示,而在主表中有,其他表中没有的数据用NULL代替。
总结
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年12月23日
2024年12月23日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]