标签:union 人力资源 group _id 条件语句 字段 笛卡尔 内连接 需要
连表查询
总是在连接的时候创建一张大表,里面存放的是两张表的笛卡尔积
在根据条件进行筛选就可以了
select * from department,employee where department.id = employee.dep_id; 当有相同的字段名时候需要制定表名。
select * from department,employee where id = employee.dep_id;
连接方式:
select * from 表1,表2 where 条件;
内连接:inner join .......on......... 后面可以直接跟条件语句
select * from 表1 Inner join 表2 on 条件; select * from department inner join employee on department.id=dep_id; select * from department as t1 inner join employee as t2 on t1.id=t2.dep_id;
子查询
外连接:
左连接 left join ...... on......
select * from 表1 left join 标2 on 条件;select * from department as t1 left join employee on t1.id = dep_id; 左表的数据都会保留
右连接 right join ......on.......
select * from 表1 right join 标2 on 条件;
select * from department as t1 right join employee on t1.id=dep_id;
全外连接 union
select * from department as t1 left join employee on t1.id = dep_id; union select * from department as t1 right join employee on t1.id=dep_id;
1.找到技术部的所有人的姓名
select t1.name as department,group_concat(t2.name) from department as t1 left join employee as t2 on t1.id=t2.dep_id where t1.name=‘技术‘;
2.找到人力资源部的年龄大于40岁的人的姓名。
select t1.name as department,group_concat(t2.name) from department as t1 left join employee as t2 on t1.id=t2.dep_id where t1.name=‘人力资源‘ and t2.age>40;
标签:union 人力资源 group _id 条件语句 字段 笛卡尔 内连接 需要
原文地址:https://www.cnblogs.com/ch2020/p/12909849.html