MySQL之多表查询

Posted 一抹浅笑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
创建表和数据

 

1
2
#多表查询语法
select  字段1,字段2... from 表1,表2... [where 条件]

注意: 如果不加条件直接进行查询,则会出现以下效果,这种结果我们称之为 笛卡尔乘积

1
2
#查询人员和部门所有信息
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 | 明教   |
+----+----------+-----+-----+--------+------+-----+--------+
笛卡尔成绩示例

 

 

 

1
2
3
4
#查询人员和部门所有信息
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
View Code

 

二 多表连接查询

1
2
3
4
#多表连接查询语法(重点)
SELECT 字段列表
    FROM 表1  INNER|LEFT|RIGHT JOIN  表2
ON 表1.字段 = 表2.字段;

  

1 内连接查询 (只显示符合条件的数据)

1
2
#查询人员和部门所有信息
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 以上是关于MySQL之多表查询的主要内容,如果未能解决你的问题,请参考以下文章

MySQL之多表查询

MySQL之多表查询

mysql数据操作之多表查询

mysql之多表查询

mysql之多表查询,pymysql模块

mysql记录查询之多表查询