Home >Database >Mysql Tutorial >How to query two tables in mysql?
Mysql two-table query method: 1. Use "select field list from table 1, table 2 [where condition]" to query; 2. Use "SELECT field list FROM table 1 keyword JOIN table 2 ON Table 1. Field = Table 2. Field;" to query.
How to query two tables in mysql? The following article will introduce to you how to perform multi-table queries in MySQL. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
#创建表和数据 #创建部门 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);
Multiple table query syntax
select 字段1,字段2... from 表1,表2... [where 条件]
Note: If you do not add conditions directly When querying, the following effect will appear. This result is called Cartesian product
#查询人员和部门所有信息 select * from person,dept
Cartesian product formula: Number of data items in table A * Number of data items in table B = Cartesian product.
#笛卡尔乘积示例 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;
#Note: When querying multiple tables, be sure to find the related fields in the two tables and use them as conditions
Example
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 表2 ON 表1.字段 = 表2.字段;
1 Inner join query (only display data that meets the conditions)
#查询人员和部门所有信息 select * from person inner join dept on person.did =dept.did;
Effect: You may know It is found that the effect of inner join query and multi-table joint query is the same.
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 Left outer join query (data in the left table takes priority Show all)
#查询人员和部门所有信息 select * from person left join dept on person.did =dept.did;
Effect: All data in the personnel table are displayed, and only the data in the department table that meets the conditions will be displayed, and those that do not meet the conditions will be filled with 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 Right outer join query (all data in the right table is displayed first)
#查询人员和部门所有信息 select * from person right join dept on person.did =dept.did;
Effect: exactly the same as [left The opposite of outer join]
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 Full join query (displays all data in the left and right tables)
Full join query: It is based on the inner join and adds no left and right sides Displayed data
Note: mysql does not support the full JOIN keyword
Note: However, mysql provides the UNION keyword. Use UNION to indirectly implement the full JOIN function
#查询人员和部门的所有数据 SELECT * FROM person LEFT JOIN dept ON person.did = dept.did UNION SELECT * FROM person RIGHT JOIN dept ON person.did = dept.did;
Example
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
Note: The difference between UNION and UNION ALL: UNION will remove duplicate data, while UNION ALL will directly display the results
1. Query Employees who leave the teaching department and are older than 20 years old and whose salary is less than 40,000 are arranged in reverse order of salary. (Requirement: use multi-table joint query and inner join query respectively)
Example
#1.多表联合查询方式: select * from person p1,dept d2 where p1.did = d2.did and d2.dname='python' and age>20 and salary 6711a057ef8e38c8a07a7a48b0f1529c20 and salary e7070b225b4214faf7341bd5f65864b3, 5d73f91be322fe8af7d729e857196096Note: The table name after as cannot be enclosed in quotation marks ( '')<br><h5> <strong>2. Find the name and salary of the person with the maximum salary</strong> </h5><pre class="brush:php;toolbar:false">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);
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);
Recommended tutorial: mysql video tutorial
The above is the detailed content of How to query two tables in mysql?. For more information, please follow other related articles on the PHP Chinese website!