golangmysql多表查询_MySQL多表查询⼀ 多表连接查询
#重点:外链接语法
SELECT 字段列表
FROM 表1 INNER|LEFT|RIGHT JOIN 表2
ON 表1.字段= 表2.字段;
#建表
create table department(
id int,
设备接口类型有哪些name varchar(20)
)
;
create table employee(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);#插⼊数据
insert into department values
(200,'技术'),
(201,'⼈⼒资源'),
(202,'销售'),
(203,'运营');
insert into employee(name,sex,age,dep_id) values
('egon','male',18,200),
('alex','female',48,201),
('wupeiqi','male',38,201),
('yuanhao','female',28,202),
('liwenzhou','male',18,200),
('jingliyang','female',18,204)
;#查看表结构和数据
mysql>desc department;+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+mysql>desc employee;+--------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| sex | enum('male','female') | NO | | male | |
| age | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | | NULL | |
+--------+-----------------------+------+-----+---------+----------------+mysql> select * fromdepartment;+------+--------------+
| id | name |
+------+--------------+
| 200 | 技术 |
| 201 | ⼈⼒资源 |
| 202 | 销售 |
| 203 | 运营 |
+------+--------------+mysql> select * fromemployee;+----+------------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+------------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | female | 18 | 204 |
+----+------------+--------+------+--------+表department与employee
准备好表
1 交叉连接:不适⽤任何匹配条件。⽣成笛卡尔积
2 内连接:只连接匹配的⾏
#两张表共有的部分,相当于利⽤条件从笛卡尔积结果中筛选出了正确的结果#department没有204这个部门,因⽽employee表中关于204这条员⼯信息没有匹配出来
mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee inner join department on employee.dep_id=department.id;+----+-----------+------+--------+--------------+
| id | name | age | sex | name |
+----+-----------+------+--------+--------------+
| 1 | gouge | 18 | male | 技术 |
| 2 | 华仔 | 48 | female | ⼈⼒资源 |
| 3 | ⽼王 | 38 | male | ⼈⼒资源 |
| 4 | 刘哥 | 28 | female | 销售 |
| 5 | jjc | 18 | male | 技术 |
+----+-----------+------+--------+--------------+
#上述sql等同于
mysql> select employee.id,employee.name,employee.age,employee.sex,department.name from employee,department where employee.dep_id=department.id;
3 外链接之左连接:优先显⽰左表全部记录
#以左表为准,即出所有员⼯信息,当然包括没有部门的员⼯#本质就是:在内连接的基础上增加左边有右边没有的结果
mysql> select employee.id,employee.name,department.name as depart_name from employee left join department on employee.dep_id=department.id;+----+------------+--------------+
| id | name | depart_name |
+----+------------+--------------+
onmousemove鼠标事件div| 1 | gouge | 技术 |
| 5 | ⼩明 | 技术 |
| 2 | ⽼王 | ⼈⼒资源 |
forbidden to do还是doing| 3 | 刘哥 | ⼈⼒资源 |
| 4 | jjc | 销售 |
| 6 | ⿊⼈ | NULL |
+----+------------+--------------+
4 外链接之右连接:优先显⽰右表全部记录
#以右表为准,即出所有部门信息,包括没有员⼯的部门#本质就是:在内连接的基础上增加右边有左边没有的结果
mysql> select employee.id,employee.name,department.name as depart_name from employee right join department on employee.dep_id=department.id;+------+-----------+--------------+
| id | name | depart_name |
+------+-----------+--------------+
| 1 | gouge | 技术 |
| 2 | laowang | ⼈⼒资源 |
| 3 | jjc | ⼈⼒资源 |
| 4 | 刘哥 | 销售 |
| 5 | ⽂⽂ | 技术 |
| NULL | NULL | 运营 |
+------+-----------+--------------+
5 全外连接:显⽰左右两个表全部记录
全外连接:在内连接的基础上增加左边有右边没有的和右边有左边没有的结果#注意:mysql不⽀持全外连接 full JOIN#强调:mysql可以使⽤此种⽅式间接实现全外连接
select * from employee left join department on employee.dep_id =department.id
unionmysql语句多表查询
select* from employee right join department on employee.dep_id =department.id
important后加什么
;#查看结果
+------+------------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+------+------------+--------+------+--------+------+--------------+
| 1 | gouge | male | 18 | 200 | 200 | 技术 |
| 5 | jjc | male | 18 | 200 | 200 | 技术 |
| 2 | ⽼王 | female | 48 | 201 | 201 | ⼈⼒资源 |
| 3 | 华仔 | male | 38 | 201 | 201 | ⼈⼒资源 |
| 4 | ⼩明 | female | 28 | 202 | 202 | 销售 |
| 6 | jjz | female | 18 | 204 | NULL | NULL |
| NULL | NULL | NULL | NULL | NULL | 203 | 运营 |
+------+------------+--------+------+--------+------+--------------+
#注意 union与union all的区别:union会去掉相同的纪录
三 符合条件连接查询
#⽰例1:以内连接的⽅式查询employee和department表,并且employee表中的age字段值必须⼤于25,即出年龄⼤于25岁的员⼯以及员⼯所在的部门
select employee.name,department.name fromemployee inner join department
on employee.dep_id=department.id
where age> 25;#⽰例2:以内连接的⽅式查询employee和department表,并且以age字段的升序⽅式显⽰
select employee.id,employee.name,employee.age,department.name fromemployee,department
where employee.dep_id=department.idand age > 25order by age asc;
四 ⼦查询
#1:⼦查询是将⼀个查询语句嵌套在另⼀个查询语句中。#2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。#3:⼦查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字#4:还可以包含⽐较运算符:= 、 !=、> 、
1 带IN关键字的⼦查询
#查询平均年龄在25岁以上的部门名
select id,name fromdepartment
where idin(select dep_idfrom employee group by dep_id having avg(age) > 25);#查看技术部员⼯姓名
select name fromemployee
where dep_idin(select idfrom department where name='技术');#查看不⾜1⼈的部门名(⼦查询得到的是有⼈的部门id) select name from department where id not in (select distinct dep_id from employee);
2 带⽐较运算符的⼦查询
#⽐较运算符:=、!=、>、>=、#查询⼤于所有⼈平均年龄的员⼯名与年龄
mysql> select name,age from emp where age > (select avg(age) fromemp);#查询⼤于部门内平均年龄的员⼯名、年龄select t1.name,t1.age fromemp t1
inner join
(select dep_id,avg(age) avg_agefromemp group by dep_id) t2
on t1.dep_id=t2.dep_id
where t1.age> t2.avg_age;
3 带EXISTS关键字的⼦查询
EXISTS关字键字表⽰存在。在使⽤EXISTS关键字时,内层查询语句不返回查询的记录。
⽽是返回⼀个真假值。True或False
ssh框架下载
当返回True时,外层查询语句将进⾏查询;当返回值为False时,外层查询语句不进⾏查询
#department表中存在dept_id=203,Ture
mysql> select * fromemployee->where exists-> (select id from department where id=200);
#department表中存在dept_id=205,False
mysql> select * from employee
-> where exists
-> (select id from department where id=204);
Empty set (0.00 sec)
⼀切的⼀切取决于sql语句的执⾏顺序!!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。