mysql怎样两表查询?下面本篇文章给大家介绍一下mysql中进行多表查询的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
多表联合查询#创建表和数据#创建部门create table if not exists dept ( did int not null auto_increment primary key, dname varchar(50) not null comment '部门名称')engine=innodb default charset utf8;#添加部门数据insert into `dept` values ('1', '教学部');insert into `dept` values ('2', '销售部');insert into `dept` values ('3', '市场部');insert into `dept` values ('4', '人事部');insert into `dept` values ('5', '鼓励部');-- 创建人员drop table if exists `person`;create table `person` ( `id` int(11) not null auto_increment, `name` varchar(50) not null, `age` tinyint(4) default '0', `sex` enum('男','女','人妖') not null default '人妖', `salary` decimal(10,2) not null default '250.00', `hire_date` date not null, `dept_id` int(11) default null, primary key (`id`)) engine=innodb auto_increment=13 default charset=utf8;-- 添加人员数据-- 教学部insert into `person` values ('1', 'alex', '28', '人妖', '53000.00', '2010-06-21', '1');insert into `person` values ('2', 'wupeiqi', '23', '男', '8000.00', '2011-02-21', '1');insert into `person` values ('3', 'egon', '30', '男', '6500.00', '2015-06-21', '1');insert into `person` values ('4', 'jingnvshen', '18', '女', '6680.00', '2014-06-21', '1');-- 销售部insert into `person` values ('5', '歪歪', '20', '女', '3000.00', '2015-02-21', '2');insert into `person` values ('6', '星星', '20', '女', '2000.00', '2018-01-30', '2');insert into `person` values ('7', '格格', '20', '女', '2000.00', '2018-02-27', '2');insert into `person` values ('8', '周周', '20', '女', '2000.00', '2015-06-21', '2');-- 市场部insert into `person` values ('9', '月月', '21', '女', '4000.00', '2014-07-21', '3');insert into `person` values ('10', '安琪', '22', '女', '4000.00', '2015-07-15', '3');-- 人事部insert into `person` values ('11', '周明月', '17', '女', '5000.00', '2014-06-21', '4');-- 鼓励部insert into `person` values ('12', '苍老师', '33', '女', '1000000.00', '2018-02-21', null);
多表查询语法
select 字段1,字段2... from 表1,表2... [where 条件]
注意: 如果不加条件直接进行查询,则会出现以下效果,这种结果我们称之为 笛卡尔乘积
#查询人员和部门所有信息select * from person,dept
笛卡尔乘积公式 : a表中数据条数 * b表中数据条数 = 笛卡尔乘积.
#笛卡尔乘积示例mysql> select * from person ,dept;+----+----------+-----+-----+--------+------+-----+--------+| id | name | age | sex | salary | did | did | dname |+----+----------+-----+-----+--------+------+-----+--------+| 1 | alex | 28 | 女 | 53000 | 1 | 1 | python || 1 | alex | 28 | 女 | 53000 | 1 | 2 | linux || 1 | alex | 28 | 女 | 53000 | 1 | 3 | 明教 || 2 | wupeiqi | 23 | 女 | 29000 | 1 | 1 | python || 2 | wupeiqi | 23 | 女 | 29000 | 1 | 2 | linux || 2 | wupeiqi | 23 | 女 | 29000 | 1 | 3 | 明教 || 3 | egon | 30 | 男 | 27000 | 1 | 1 | python || 3 | egon | 30 | 男 | 27000 | 1 | 2 | linux || 3 | egon | 30 | 男 | 27000 | 1 | 3 | 明教 || 4 | oldboy | 22 | 男 | 1 | 2 | 1 | python || 4 | oldboy | 22 | 男 | 1 | 2 | 2 | linux || 4 | oldboy | 22 | 男 | 1 | 2 | 3 | 明教 || 5 | jinxin | 33 | 女 | 28888 | 1 | 1 | python || 5 | jinxin | 33 | 女 | 28888 | 1 | 2 | linux || 5 | jinxin | 33 | 女 | 28888 | 1 | 3 | 明教 || 6 | 张无忌 | 20 | 男 | 8000 | 3 | 1 | python || 6 | 张无忌 | 20 | 男 | 8000 | 3 | 2 | linux || 6 | 张无忌 | 20 | 男 | 8000 | 3 | 3 | 明教 || 7 | 令狐冲 | 22 | 男 | 6500 | null | 1 | python || 7 | 令狐冲 | 22 | 男 | 6500 | null | 2 | linux || 7 | 令狐冲 | 22 | 男 | 6500 | null | 3 | 明教 || 8 | 东方不败 | 23 | 女 | 18000 | null | 1 | python || 8 | 东方不败 | 23 | 女 | 18000 | null | 2 | linux || 8 | 东方不败 | 23 | 女 | 18000 | null | 3 | 明教 |+----+----------+-----+-----+--------+------+-----+--------+
#查询人员和部门所有信息select * from person,dept where person.did = dept.did;
#注意: 多表查询时,一定要找到两个表中相互关联的字段,并且作为条件使用
示例
mysql> select * from person,dept where person.did = dept.did;+----+---------+-----+-----+--------+-----+-----+--------+| id | name | age | sex | salary | did | did | dname |+----+---------+-----+-----+--------+-----+-----+--------+| 1 | alex | 28 | 女 | 53000 | 1 | 1 | python || 2 | wupeiqi | 23 | 女 | 29000 | 1 | 1 | python || 3 | egon | 30 | 男 | 27000 | 1 | 1 | python || 4 | oldboy | 22 | 男 | 1 | 2 | 2 | linux || 5 | jinxin | 33 | 女 | 28888 | 1 | 1 | python || 6 | 张无忌 | 20 | 男 | 8000 | 3 | 3 | 明教 || 7 | 令狐冲 | 22 | 男 | 6500 | 2 | 2 | linux |+----+---------+-----+-----+--------+-----+-----+--------+7 rows in set
多表链接查询#多表连接查询语法(重点)select 字段列表 from 表1 inner|left|right join 表2on 表1.字段 = 表2.字段;
1 内连接查询 (只显示符合条件的数据)
#查询人员和部门所有信息select * from person inner join dept on person.did =dept.did;
效果: 大家可能会发现, 内连接查询与多表联合查询的效果是一样的.
mysql> select * from person inner join dept on person.did =dept.did;+----+---------+-----+-----+--------+-----+-----+--------+| id | name | age | sex | salary | did | did | dname |+----+---------+-----+-----+--------+-----+-----+--------+| 1 | alex | 28 | 女 | 53000 | 1 | 1 | python || 2 | wupeiqi | 23 | 女 | 29000 | 1 | 1 | python || 3 | egon | 30 | 男 | 27000 | 1 | 1 | python || 4 | oldboy | 22 | 男 | 1 | 2 | 2 | linux || 5 | jinxin | 33 | 女 | 28888 | 1 | 1 | python || 6 | 张无忌 | 20 | 男 | 8000 | 3 | 3 | 明教 || 7 | 令狐冲 | 22 | 男 | 6500 | 2 | 2 | linux |+----+---------+-----+-----+--------+-----+-----+--------+7 rows in set
2 左外连接查询 (左边表中的数据优先全部显示)
#查询人员和部门所有信息select * from person left join dept on person.did =dept.did;
效果:人员表中的数据全部都显示,而 部门表中的数据符合条件的才会显示,不符合条件的会以 null 进行填充.
mysql> select * from person left join dept on person.did =dept.did;+----+----------+-----+-----+--------+------+------+--------+| id | name | age | sex | salary | did | did | dname |+----+----------+-----+-----+--------+------+------+--------+| 1 | alex | 28 | 女 | 53000 | 1 | 1 | python || 2 | wupeiqi | 23 | 女 | 29000 | 1 | 1 | python || 3 | egon | 30 | 男 | 27000 | 1 | 1 | python || 5 | jinxin | 33 | 女 | 28888 | 1 | 1 | python || 4 | oldboy | 22 | 男 | 1 | 2 | 2 | linux || 7 | 令狐冲 | 22 | 男 | 6500 | 2 | 2 | linux || 6 | 张无忌 | 20 | 男 | 8000 | 3 | 3 | 明教 || 8 | 东方不败 | 23 | 女 | 18000 | null | null | null |+----+----------+-----+-----+--------+------+------+--------+8 rows in set
3 右外连接查询 (右边表中的数据优先全部显示)
#查询人员和部门所有信息select * from person right join dept on person.did =dept.did;
效果:正好与[左外连接相反]
mysql> select * from person right join dept on person.did =dept.did;+----+---------+-----+-----+--------+-----+-----+--------+| id | name | age | sex | salary | did | did | dname |+----+---------+-----+-----+--------+-----+-----+--------+| 1 | alex | 28 | 女 | 53000 | 1 | 1 | python || 2 | wupeiqi | 23 | 女 | 29000 | 1 | 1 | python || 3 | egon | 30 | 男 | 27000 | 1 | 1 | python || 4 | oldboy | 22 | 男 | 1 | 2 | 2 | linux || 5 | jinxin | 33 | 女 | 28888 | 1 | 1 | python || 6 | 张无忌 | 20 | 男 | 8000 | 3 | 3 | 明教 || 7 | 令狐冲 | 22 | 男 | 6500 | 2 | 2 | linux |+----+---------+-----+-----+--------+-----+-----+--------+7 rows in set
4 全连接查询(显示左右表中全部数据)
全连接查询:是在内连接的基础上增加 左右两边没有显示的数据
注意: mysql并不支持全连接 full join 关键字
注意: 但是mysql 提供了 union 关键字.使用 union 可以间接实现 full join 功能
#查询人员和部门的所有数据 select * from person left join dept on person.did = dept.didunionselect * from person right join dept on person.did = dept.did;
示例
mysql> select * from person left join dept on person.did = dept.did union select * from person right join dept on person.did = dept.did;+------+----------+------+------+--------+------+------+--------+| id | name | age | sex | salary | did | did | dname |+------+----------+------+------+--------+------+------+--------+| 1 | alex | 28 | 女 | 53000 | 1 | 1 | python || 2 | wupeiqi | 23 | 女 | 29000 | 1 | 1 | python || 3 | egon | 30 | 男 | 27000 | 1 | 1 | python || 5 | jinxin | 33 | 女 | 28888 | 1 | 1 | python || 4 | oldboy | 22 | 男 | 1 | 2 | 2 | linux || 7 | 令狐冲 | 22 | 男 | 6500 | 2 | 2 | linux || 6 | 张无忌 | 20 | 男 | 8000 | 3 | 3 | 明教 || 8 | 东方不败 | 23 | 女 | 18000 | null | null | null || null | null | null | null | null | null | 4 | 基督教 |+------+----------+------+------+--------+------+------+--------+9 rows in set
注意: union 和 union all 的区别:union 会去掉重复的数据,而 union all 则直接显示结果
复制条件多表查询1、查询出 教学部 年龄大于20岁,并且工资小于40000的员工,按工资倒序排列.(要求:分别使用多表联合查询和内连接查询)
示例
#1.多表联合查询方式:select * from person p1,dept d2 where p1.did = d2.did and d2.dname='python' and age>20 and salary <40000 order by salary desc;#2.内连接查询方式:select * from person p1 inner join dept d2 on p1.did= d2.did and d2.dname='python' and age>20 and salary <40000 order by salary desc;
2、查询每个部门中最高工资和最低工资是多少,显示部门名称
select max(salary),min(salary),dept.dname from person left join dept on person.did = dept.did group by person.did;
子语句查询子查询(嵌套查询): 查多次, 多个select
注意: 第一次的查询结果可以作为第二次的查询的 条件 或者 表名 使用.
子查询中可以包含:in、not in、any、all、exists 和 not exists等关键字. 还可以包含比较运算符:= 、 !=、> 、<等.
1、作为表名使用select * from (select * from person) as 表名;
ps:大家需要注意的是: 一条语句中可以有多个这样的子查询,在执行时,最里层括号(sql语句) 具有优先执行权.<br>注意: as 后面的表名称不能加引号('')
2.求最大工资那个人的姓名和薪水1.求最大工资select max(salary) from person;2.求最大工资那个人叫什么select name,salary from person where salary=53000;合并select name,salary from person where salary=(select max(salary) from person);
3. 求工资高于所有人员平均工资的人员1.求平均工资select avg(salary) from person;2.工资大于平均工资的 人的姓名、工资select name,salary from person where salary > 21298.625;合并select name,salary from person where salary >(select avg(salary) from person);
推荐教程:mysql视频教程
以上就是mysql怎样两表查询?的详细内容。
