立即注册 登录
About云-梭伦科技 返回首页

he280166068的个人空间 https://aboutyun.com/?46359 [收藏] [复制] [分享] [RSS]

日志

mysql多表联查练习

已有 1757 次阅读2016-9-27 17:41 |个人分类:mysql数据库练习| mysql

内连接
mysql> select * from emp inner join dept on emp.emp_id=dept.emp_id;
+--------+------+--------+------+
| emp_id | name | emp_id | name |
+--------+------+--------+------+
|      1 | aa   |      1 | hh   |
|      2 | bb   |      2 | dd   |
+--------+------+--------+------+
2 rows in set (0.00 sec)


左连接:
mysql> select * from emp left join dept on emp.emp_id=dept.empt_id;
ERROR 1054 (42S22): Unknown column 'dept.empt_id' in 'on clause'
mysql> select * from emp left join dept on emp.emp_id=dept.emp_id;
+--------+------+--------+------+
| emp_id | name | emp_id | name |
+--------+------+--------+------+
|      1 | aa   |      1 | hh   |
|      2 | bb   |      2 | dd   |
|      3 | cc   |   NULL | NULL |
+--------+------+--------+------+
3 rows in set (0.00 sec)

右连接
mysql> select * from emp right join dept on emp.emp_id=dept.emp_id;
+--------+------+--------+------+
| emp_id | name | emp_id | name |
+--------+------+--------+------+
|      1 | aa   |      1 | hh   |
|      2 | bb   |      2 | dd   |
|   NULL | NULL |      5 | ff   |
+--------+------+--------+------+
3 rows in set (0.00 sec)

全连接:
mysql> select * from emp right join dept on emp.emp_id=dept.emp_id union
    -> select * from emp left join dept on emp.emp_id=dept.emp_id;
+--------+------+--------+------+
| emp_id | name | emp_id | name |
+--------+------+--------+------+
|      1 | aa   |      1 | hh   |
|      2 | bb   |      2 | dd   |
|   NULL | NULL |      5 | ff   |
|      3 | cc   |   NULL | NULL |
+--------+------+--------+------+
4 rows in set (0.00 sec)


三表联查:
1.利用内连接---两边都匹配的
mysql> select * from emp inner join dept on emp.emp_id=dept.emp_id inner join
    -> model on model.mo_id=emp.emp_id;
+--------+------+--------+------+-------+---------+---------+
| emp_id | name | emp_id | name | mo_id | mo_name | mo_file |
+--------+------+--------+------+-------+---------+---------+
|      1 | aa   |      1 | hh   |     1 | cc      | 0       |
|      2 | bb   |      2 | dd   |     2 | hh      | 1       |
+--------+------+--------+------+-------+---------+---------+
2 rows in set (0.00 sec)
2.直接对等---三边都匹配的。
mysql> select * from emp,dept,model where emp.emp_id=dept.emp_id and emp.emp_id
=model.mo_id;
+--------+------+--------+------+-------+---------+---------+
| emp_id | name | emp_id | name | mo_id | mo_name | mo_file |
+--------+------+--------+------+-------+---------+---------+
|      1 | aa   |      1 | hh   |     1 | cc      | 0       |
|      2 | bb   |      2 | dd   |     2 | hh      | 1       |
+--------+------+--------+------+-------+---------+---------+
2 rows in set (0.00 sec)
====================================================================================
mysql> select * from emp,dept,model where emp.emp_id=dept.emp_id=model.mo_id;
+--------+------+--------+------+-------+---------+---------+
| emp_id | name | emp_id | name | mo_id | mo_name | mo_file |
+--------+------+--------+------+-------+---------+---------+
|      1 | aa   |      1 | hh   |     1 | cc      | 0       |
|      2 | bb   |      2 | dd   |     1 | cc      | 0       |
+--------+------+--------+------+-------+---------+---------+
2 rows in set (0.00 sec)
 当emp.emp_id=dept_id确定了结果,导致全为1;

3.嵌套查询,先两表后三表
mysql> select * from model,(select emp.emp_id temp_id  from emp inner join dept
on emp.emp_id=dept.emp_id) temp where model.mo_id=temp_id;
+-------+---------+---------+---------+
| mo_id | mo_name | mo_file | temp_id |
+-------+---------+---------+---------+
|     1 | cc      | 0       |       1 |
|     2 | hh      | 1       |       2 |
+-------+---------+---------+---------+
2 rows in set (0.00 sec)




注意相同字段的问题。
mysql> select * from (select * from emp inner join dept on emp.emp_id=dept.emp_i
d) temp;
ERROR 1060 (42S21): Duplicate column name 'emp_id'
mysql> select * from (select emp.emp_id temp_id,dept.emp_id tept_name from emp i
nner join dept on emp.emp_id=dept.emp_id) temp;
+---------+-----------+
| temp_id | tept_name |
+---------+-----------+
|       1 |         1 |
|       2 |         2 |
+---------+-----------+
2 rows in set (0.00 sec)
//为什么会报重名错误,把查询结果当作了 一张表temp.

mysql> create table temp select emp.emp_id temp_id,dept.name temp_name  from emp
,dept,model where emp.emp_id=dept.emp_id and emp.emp_id =model.mo_id;
Query OK, 2 rows affected (0.07 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from temp;
+---------+-----------+
| temp_id | temp_name |
+---------+-----------+
|       1 | hh        |
|       2 | dd        |
+---------+-----------+
2 rows in set (0.00 sec)


路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关闭

推荐上一条 /2 下一条