MySQL命令【关联】
2022/3/8 19:14:53
本文主要是介绍MySQL命令【关联】,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
内连接
inner join,⼜叫内连接的部分,主要是获取两个表中字段匹配关系的表。查询关联字段共同拥有的数据
查询两个表共同拥有的数据(inner join)
mysql> use employees; Database changed mysql> select e.first_name,e.last_name,e.gender,s.salary from employees as e inner join salaries as s on e.emp_no=s.emp_no where s.salary <40000 limit 3; +------------+-----------+--------+--------+ | first_name | last_name | gender | salary | +------------+-----------+--------+--------+ | Shahaf | Famili | M | 39935 | | Divier | Reistad | F | 39520 | | Pradeep | Makrucki | M | 39765 | +------------+-----------+--------+--------+ 3 rows in set (0.01 sec)
左连接
获取左表所有记录,获取左边数据表所有符合要求的字段数据信息
获取左表所有记录(left join)
mysql> use anruo; Database changed mysql> select p.name,p.age,p.age,w.company,w.salary from person as p left join work as w on p.code=w.code limit 5; +---------+------+------+---------+--------+ | name | age | age | company | salary | +---------+------+------+---------+--------+ | zhangli | 24 | 24 | alibaba | 1000 | | acd | 23 | 23 | zczczc | 1500 | +---------+------+------+---------+--------+ 2 rows in set (0.01 sec)
三表之间的查询
mysql> select p.name,p.age,p.sex,w.company,w.salary,info.type,info.isSafe from person as p left join work as w on p.code=w.code left join workinfo as info on w.workID=info.workID limit 5; +---------+------+------+---------+--------+-----------+--------+ | name | age | sex | company | salary | type | isSafe | +---------+------+------+---------+--------+-----------+--------+ | zhangli | 24 | boy | alibaba | 1000 | Full-time | safe | | acd | 23 | boy | zczczc | 1500 | NULL | NULL | +---------+------+------+---------+--------+-----------+--------+ 2 rows in set (0.00 sec)
右链接
获取右表所有记录的信息,获取右边数据表所有的数据信息
获取右表所有记录(right join)
mysql> select w.company,w.salary,info.type,info.issafe from work as w right join workinfo as info on w.workID=info.workID; +---------+--------+-----------+--------+ | company | salary | type | issafe | +---------+--------+-----------+--------+ | alibaba | 1000 | Full-time | safe | +---------+--------+-----------+--------+ 1 row in set (0.00 sec)
通过子查询,来进行查询
mysql> select * from person where code in (select code from work where code=1001); +----+------+---------+------+------+ | id | code | name | age | sex | +----+------+---------+------+------+ | 1 | 1001 | zhangli | 24 | boy | +----+------+---------+------+------+ 1 row in set (0.00 sec)
这篇关于MySQL命令【关联】的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-02MySQL 3主集群搭建
- 2024-12-25如何部署MySQL集群资料:新手入门教程
- 2024-12-24MySQL集群部署资料:新手入门教程
- 2024-12-24MySQL集群资料详解:新手入门教程
- 2024-12-24MySQL集群部署入门教程
- 2024-12-24部署MySQL集群学习:新手入门教程
- 2024-12-24部署MySQL集群入门:一步一步搭建指南
- 2024-12-07MySQL读写分离入门:轻松掌握数据库读写分离技术
- 2024-12-07MySQL读写分离入门教程
- 2024-12-07MySQL分库分表入门详解