MySQL工作常用Select命令:多表联合查询
mysql> desc bumen; #表1
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| bumenName | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> desc yg; #表2
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | bigint(10) | NO | PRI | NULL | auto_increment |
| ygName | varchar(20) | YES | | NULL | |
| salary | float | YES | | NULL | |
| bmID | int(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+----------------+
mysql> insert into bumen values (1,'研发部'),(2,'测试部'),(3,'UI'),(4,'市场');
Query OK, 4 rows affected (0.02 sec)
mysql> insert into yg values (1,'田老板',20000,1);
Query OK, 1 row affected (0.02 sec)
mysql> insert into yg values (2,'汪总',221000,1);
Query OK, 1 row affected (0.02 sec)
mysql> insert into yg values (3,'李四',15000,2);
Query OK, 1 row affected (0.01 sec)
mysql> insert into yg values (4,'大神',30000,3);
Query OK, 1 row affected (0.02 sec)
mysql> insert into yg values (5,'销售',41321.9,4);
Query OK, 1 row affected (0.01 sec)
mysql> select * from yg;
+----+-----------+---------+------+
| id | ygName | salary | bmID |
+----+-----------+---------+------+
| 1 | 田老板 | 20000 | 1 |
| 2 | 汪总 | 221000 | 1 |
| 3 | 李四 | 15000 | 2 |
| 4 | 大神 | 30000 | 3 |
| 5 | 销售 | 41321.9 | 4 |
+----+-----------+---------+------+
5 rows in set (0.02 sec)
mysql> select * from bumen;
+----+-----------+
| id | bumenName |
+----+-----------+
| 1 | 研发部 |
| 2 | 测试部 |
| 3 | UI |
| 4 | 市场 |
+----+-----------+
4 rows in set (0.00 sec)
mysql> select * from yg y ,bumen b where y.bmID=b.id;
+----+-----------+---------+------+----+-----------+
| id | ygName | salary | bmID | id | bumenName |
+----+-----------+---------+------+----+-----------+
| 1 | 田老板 | 20000 | 1 | 1 | 研发部 |
| 2 | 汪总 | 221000 | 1 | 1 | 研发部 |
| 3 | 李四 | 15000 | 2 | 2 | 测试部 |
| 4 | 大神 | 30000 | 3 | 3 | UI |
| 5 | 销售 | 41321.9 | 4 | 4 | 市场 |
+----+-----------+---------+------+----+-----------+
5 rows in set (0.00 sec)
mysql> select b.bumenName,y.ygName from yg y ,bumen b where y.bmID=b.id;
+-----------+-----------+
| bumenName | ygName |
+-----------+-----------+
| 研发部 | 田老板 |
| 研发部 | 汪总 |
| 测试部 | 李四 |
| UI | 大神 |
| 市场 | 销售 |
+-----------+-----------+
5 rows in set (0.00 sec)
mysql> select b.bumenName,y.ygName from yg y ,bumen b where y.bmID=b.id and b.bumenName='研发部';
+-----------+-----------+
| bumenName | ygName |
+-----------+-----------+
| 研发部 | 田老板 |
| 研发部 | 汪总 |
+-----------+-----------+
2 rows in set (0.00 sec)
mysql> select * from yg y,bumen b where y.bmID=b.id;
+----+-----------+---------+------+----+-----------+
| id | ygName | salary | bmID | id | bumenName |
+----+-----------+---------+------+----+-----------+
| 1 | 田老板 | 20000 | 1 | 1 | 研发部 |
| 2 | 汪总 | 221000 | 1 | 1 | 研发部 |
| 3 | 李四 | 15000 | 2 | 2 | 测试部 |
| 4 | 大神 | 30000 | 3 | 3 | UI |
| 5 | 销售 | 41321.9 | 4 | 4 | 市场 |
+----+-----------+---------+------+----+-----------+
5 rows in set (0.00 sec)
mysql> select * from yg y,bumen b,y.salary where y.bmID=b.id and b.bumenName='研发部';
ERROR 1049 (42000): Unknown database 'y'
mysql> select * from yg y,bumen b where y.bmID=b.id and b.bumenName='研发部';
+----+-----------+--------+------+----+-----------+
| id | ygName | salary | bmID | id | bumenName |
+----+-----------+--------+------+----+-----------+
| 1 | 田老板 | 20000 | 1 | 1 | 研发部 |
| 2 | 汪总 | 221000 | 1 | 1 | 研发部 |
+----+-----------+--------+------+----+-----------+
2 rows in set (0.00 sec)
mysql> select y.ygName,y.salary,b.bumenName from yg y,bumen b where y.bmID=b.id and b.bumenName='研发部';
+-----------+--------+-----------+
| ygName | salary | bumenName |
+-----------+--------+-----------+
| 田老板 | 20000 | 研发部 |
| 汪总 | 221000 | 研发部 |
+-----------+--------+-----------+
2 rows in set (0.00 sec)
mysql> select y.ygName,y.salary,y.bmID,b.bumenName from bumen b,yg y where b.id=y.bmID and b.bumenName='研发部' or b.bumenName='市场';
+-----------+---------+------+-----------+
| ygName | salary | bmID | bumenName |
+-----------+---------+------+-----------+
| 田老板 | 20000 | 1 | 研发部 |
| 田老板 | 20000 | 1 | 市场 |
| 汪总 | 221000 | 1 | 研发部 |
| 汪总 | 221000 | 1 | 市场 |
| 李四 | 15000 | 2 | 市场 |
| 大神 | 30000 | 3 | 市场 |
| 销售 | 41321.9 | 4 | 市场 |
| 田总 | 230000 | 1 | 研发部 |
| 田总 | 230000 | 1 | 市场 |
| 胡总 | 210000 | 4 | 市场 |
+-----------+---------+------+-----------+
10 rows in set (0.00 sec)
mysql> select y.ygName,y.salary,y.bmID,b.bumenName from bumen b,yg y where b.id=y.bmID and (b.bumenName='研发部' or b.bumenName='市场');
+-----------+---------+------+-----------+
| ygName | salary | bmID | bumenName |
+-----------+---------+------+-----------+
| 田老板 | 20000 | 1 | 研发部 |
| 汪总 | 221000 | 1 | 研发部 |
| 销售 | 41321.9 | 4 | 市场 |
| 田总 | 230000 | 1 | 研发部 |
| 胡总 | 210000 | 4 | 市场 |
+-----------+---------+------+-----------+
mysql> select y.ygName,y.salary,y.bmID,b.bumenName from bumen b,yg y where b.id=y.bmID and b.bumenName='研发部'
-> union
-> select y.ygName,y.salary,y.bmID,b.bumenName from bumen b,yg y where b.id=y.bmID and b.bumenName='市场';
+-----------+---------+------+-----------+
| ygName | salary | bmID | bumenName |
+-----------+---------+------+-----------+
| 田老板 | 20000 | 1 | 研发部 |
| 汪总 | 221000 | 1 | 研发部 |
| 田总 | 230000 | 1 | 研发部 |
| 销售 | 41321.9 | 4 | 市场 |
| 胡总 | 210000 | 4 | 市场 |
+-----------+---------+------+-----------+
5 rows in set (0.05 sec)
mysql> select y.ygName,y.salary,y.bmID,b.bumenName from bumen b,yg y where b.id=y.bmID and b.bumenName='研发部'
union all
select y.ygName,y.salary,y.bmID,b.bumenNName from bumen b,yg y where b.id=y.bmID and b.bumenName='市场';
+-----------+---------+------+-----------+
| ygName | salary | bmID | bumenName |
+-----------+---------+------+-----------+
| 田老板 | 20000 | 1 | 研发部 |
| 汪总 | 221000 | 1 | 研发部 |
| 田总 | 230000 | 1 | 研发部 |
| 销售 | 41321.9 | 4 | 市场 |
| 胡总 | 210000 | 4 | 市场 |
+-----------+---------+------+-----------+
↓↓ 点击"阅读原文" 【加入DevOps运维团】
相关阅读:
请转发到朋友圈并扫描关注