标签:char 复合 table order 查询 dup 连接 需要 var
多表查询
mysql> create table employee6(
-> emp_id int auto_increment primary key,
-> emp_name varchar(50),
-> age int,
-> dept_id int);
Query OK, 0 rows affected (0.18 sec)
mysql> create table department6(
-> dept_id int,
-> dept_name varchar(50));
Query OK, 0 rows affected (0.17 sec)
mysql> insert into employee6(emp_name,age,dept_id) values
-> ('tianyun',19,200),
-> ('tom',26,201),
-> ('jack',30,202),
-> ('alice',24,203),
-> ('robin',40,204),
-> ('narasha',28,205);
Query OK, 6 rows affected (0.01 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> insert into department6 values(200,'hr'), (201,'it'), (202,'sale'), (203,'fd');
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from employee6;
+--------+----------+------+---------+
| emp_id | emp_name | age | dept_id |
+--------+----------+------+---------+
| 1 | tianyun | 19 | 200 |
| 2 | tom | 26 | 201 |
| 3 | jack | 30 | 202 |
| 4 | alice | 24 | 203 |
| 5 | robin | 40 | 204 |
| 6 | narasha | 28 | 205 |
+--------+----------+------+---------+
6 rows in set (0.00 sec)
mysql> select * from department6;
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| 200 | hr |
| 201 | it |
| 202 | sale |
| 203 | fd |
+---------+-----------+
4 rows in set (0.00 sec)
mysql>select employee6.emp_id,employee6.emp_name, employee6.age, department6.dept_name
->from employee6, department6
->where employee66.dept_id = department6.dept_id;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 1 | tianyun | 19 | hr |
| 2 | tom | 26 | it |
| 3 | jack | 30 | sale |
| 4 | alice | 24 | fd |
+--------+----------+------+-----------+
4 rows in set (0.00 sec)
可以简写为,但是如果该字段两张表都存在,则不能简写
mysql> select emp_id,emp_name,age,dept_name from employee6, department6 where employee6.dept_id = department6.dept_id;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 1 | tianyun | 19 | hr |
| 2 | tom | 26 | it |
| 3 | jack | 30 | sale |
| 4 | alice | 24 | fd |
+--------+----------+------+-----------+
4 rows in set (0.00 sec)
发现只显示了有部门的员工
mysql> select emp_id,emp_name,age,dept_name from employee6 left join department6 on emplooyee6
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 1 | tianyun | 19 | hr |
| 2 | tom | 26 | it |
| 3 | jack | 30 | sale |
| 4 | alice | 24 | fd |
| 5 | robin | 40 | NULL |
| 6 | narasha | 28 | NULL |
+--------+----------+------+-----------+
6 rows in set (0.00 sec)
mysql> insert into department6 values(510,'os');
Query OK, 1 row affected (0.04 sec)
mysql> select emp_id,emp_name,age,dept_name from employee6 right join department6 on emplloyee6
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 1 | tianyun | 19 | hr |
| 2 | tom | 26 | it |
| 3 | jack | 30 | sale |
| 4 | alice | 24 | fd |
| NULL | NULL | NULL | os |
+--------+----------+------+-----------+
5 rows in set (0.00 sec)
找出公司部门中年龄大于25岁的员工
mysql> select emp_id,emp_name,age,dept_name from employee6, department6 where employee6.dept_id = department6.dept_id and age>=25;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 2 | tom | 26 | it |
| 3 | jack | 30 | sale |
+--------+----------+------+-----------+
2 rows in set (0.00 sec)
mysql> select emp_id,emp_name,age,dept_name from employee6, department6 where employee6.dept_id = department6.dept_id order by age;
+--------+----------+------+-----------+
| emp_id | emp_name | age | dept_name |
+--------+----------+------+-----------+
| 1 | tianyun | 19 | hr |
| 4 | alice | 24 | fd |
| 2 | tom | 26 | it |
| 3 | jack | 30 | sale |
+--------+----------+------+-----------+
4 rows in set (0.00 sec)
标签:char 复合 table order 查询 dup 连接 需要 var
原文地址:https://www.cnblogs.com/inmeditation/p/11660757.html