千家信息网

mysql两表查询的案例分析

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,小编给大家分享一下mysql两表查询的案例分析,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!mysql两表查询的方法:1、使用"select 字段列表 from 表1,表2 [where
千家信息网最后更新 2025年02月05日mysql两表查询的案例分析

小编给大家分享一下mysql两表查询的案例分析,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

mysql两表查询的方法:1、使用"select 字段列表 from 表1,表2 [where 条件]"进行查询;2、使用"SELECT 字段列表 FROM 表1 关键字 JOIN 表2 ON 表1.字段 = 表2.字段;"进行查询。

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语句) 具有优先执行权.
注意: 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两表查询的案例分析有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

0