多对多表的连接关系
来源:互联网 发布:软件如何做授权加密 编辑:程序博客网 时间:2024/04/27 07:32
//建立三张表,学生表、课程表和中间表
//找出学生和和课程之间的联系
mysql> select * from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id ;
+----+--------+----------------------------------+-----+------+------+------+------------+-------+
| id | name | password | cid | uid | coid | id | coursename | score |
+----+--------+----------------------------------+-----+------+------+------+------------+-------+
| 1 | 曾九峰 | 4bec52e77ce530aa470e302ef2d52556 | 2 | 1 | 3 | 3 | PHP | 6 |
| 1 | 曾九峰 | 4bec52e77ce530aa470e302ef2d52556 | 2 | 1 | 4 | 4 | JS | 4 |
| 1 | 曾九峰 | 4bec52e77ce530aa470e302ef2d52556 | 2 | 1 | 2 | 2 | HTML | 3 |
| 2 | 林天福 | 4bec52e77ce530aa470e302ef2d52556 | 1 | 2 | 1 | 1 | PS | 3 |
| 3 | 杨春辉 | 4bec52e77ce530aa470e302ef2d52556 | 1 | 3 | 2 | 2 | HTML | 3 |
| 5 | 曾建平 | 4bec52e77ce530aa470e302ef2d52556 | 2 | 5 | 2 | 2 | HTML | 3 |
| 4 | 郭海鹏 | 4bec52e77ce530aa470e302ef2d52556 | 1 | 4 | 3 | 3 | PHP | 6 |
| 4 | 郭海鹏 | 4bec52e77ce530aa470e302ef2d52556 | 1 | 4 | 2 | 2 | HTML | 3 |
| 3 | 杨春辉 | 4bec52e77ce530aa470e302ef2d52556 | 1 | 3 | 3 | 3 | PHP | 6 |
| 2 | 林天福 | 4bec52e77ce530aa470e302ef2d52556 | 1 | 2 | 4 | 4 | JS | 4 |
| 6 | 马志斌 | 4bec52e77ce530aa470e302ef2d52556 | 5 | NULL | NULL | NULL | NULL | NULL |
+----+--------+----------------------------------+-----+------+------+------+------------+-------+
11 rows in set (0.00 sec)
//找出海鹏的所有信息
mysql> select * from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where stu.id=4;
+----+--------+----------------------------------+-----+------+------+------+------------+-------+
| id | name | password | cid | uid | coid | id | coursename | score |
+----+--------+----------------------------------+-----+------+------+------+------------+-------+
| 4 | 郭海鹏 | 4bec52e77ce530aa470e302ef2d52556 | 1 | 4 | 2 | 2 | HTML | 3 |
| 4 | 郭海鹏 | 4bec52e77ce530aa470e302ef2d52556 | 1 | 4 | 3 | 3 | PHP | 6 |
+----+--------+----------------------------------+-----+------+------+------+------------+-------+
2 rows in set (0.00 sec)
mysql> select coursename from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where stu.id=4;
+------------+
| coursename |
+------------+
| HTML |
| PHP |
+------------+
2 rows in set (0.00 sec)
//检索出和海鹏都学一样课程的学生
mysql> select stu.name from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where course.coursename=( sel
ect coursename from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where stu.id=4);
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> select stu.name from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where coursename=( select cou
rsename from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where stu.id=4);
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> select stu.name from stu where coursename=( select coursename from stu left join electives on stu.id=electives.uid left join course on elective
s.coid=course.id where stu.id=4);
ERROR 1054 (42S22): Champ 'coursename' inconnu dans where clause
mysql> select stu.name from stu where course.coursename=( select coursename from stu left join electives on stu.id=electives.uid left join course on e
lectives.coid=course.id where stu.id=4);
ERROR 1054 (42S22): Champ 'course.coursename' inconnu dans where clause
mysql> select stu.name from stu where coursename=( select coursename from stu left join electives on stu.id=electives.uid left join course on elective
s.coid=course.id where stu.id=4);
ERROR 1054 (42S22): Champ 'coursename' inconnu dans where clause
mysql> select stu.name from stu left join electives on stu.id=electives.uid left join course on elective.coid=course.id where coursename=( select cou
rsename from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where stu.id=4);
ERROR 1054 (42S22): Champ 'elective.coid' inconnu dans on clause
mysql> select stu.name from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where coursename=( select co
ursename from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where stu.id=4);
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> select stu.name from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where coursename in ( select
coursename from stu left join electives on stu.id=electives.uid left join course on electives.coid=course.id where stu.id=4);
+--------+
+--------+
| name |
+--------+
| 曾九峰 |
| 曾九峰 |
| 杨春辉 |
| 曾建平 |
| 郭海鹏 |
| 郭海鹏 |
| 杨春辉 |
+--------+
7 rows in set (0.00 sec)
//注意:当我们的结果条件是多个时(如上面获取的是html和php),不能用“=“进行比较,应该用”in“;
- 多对多表的连接关系
- 数据库建表多对多关系的连接
- Hibernate关联关系映射-----基于连接表的单向多对多映射配置
- Hibernate关联关系配置-----基于连接表的双向一对多/多对一映射配置
- Hibernate关联关系配置-----基于连接表的双向多对多配置
- 数据库多对多关系的表如何采用左连接查询
- Hibernate关联关系配置-----基于连接表的双向多对多配置
- Hibernate的关联关系注解映射(一对一、一对多、多对一、多对多、自连接)
- MyBatis--关联关系查询--多对多--多表连接查询
- MyBatis--关联关系查询--多对一--多表连接查询
- 关系数据库—多对多的关系
- Hibernate之多对多关系之间的关系映射
- 数据库的关系表:一对多、多对多实例
- 多对多关系的多表关联查询
- django 多对多关系的操作 包含映射表
- Hibernate中的表的多对多关系及操作
- Hibernate中表与表的多对多关系
- 多对多关系
- go语言的os.exec包介绍
- 蓝牙讲解上--搜索、配对
- 多位数与一位数相乘
- IOS使用Jenkins进行持续集成
- 大数加法、大数乘法、大数减法。Swift。
- 多对多表的连接关系
- codeforces 659 D. Bicycle Race
- 分库分表
- Android中线程同步
- V4L2视频采集驱动框架
- s5pv210 jpeg硬件编码
- 01-复杂度1 最大子列和问题
- shiro 第二节 身份验证
- HBase2.0的可用性与一致性