首页 > 其他分享 >spl多表查询

spl多表查询

时间:2022-11-28 19:57:12浏览次数:33  
标签:多表 查询 dept spl emp where id select

1.多表查询概念:从多张表查询数据

2.分类:连接查询和子查询。

有两张表A、B

连接查询: 内连接 相当于查询A、B交集数据

例1:select * from emp,dept where emp.dep_id=dept.id;

外连接 左外连接:相当于查询A表所有数据和交集部分数据

右外连接:相当于查询B表所有数据和交集部分数据

子查询:查询中嵌套查询。

3.内连接(同时查询两张表的列)

隐式内连接 select 字段列表 from 表一,表二 where 条件;

如上面的例子:select * from emp,dept where emp.dep_id=dept.id;

在没有条件语句的情况下返回笛卡尔积(排列组合)

例2:select emp.name,emp.gender,dept.name from emp,dept where emp.dep_id=dept.id;

显式内连接 select 字段列表 from 表一 inner join 表二 on 条件;

例3:select * from emp iennr join dept on emp.dep_id = dept.id; innner可以省略

4.外连接

左外连接 select 字段列表 from 表1 left join 表2 on 条件;

例4:查询emp表所有数据和对应的部门信息

select * from emp left join dept on emp.dep_id = dept.id;

右外连接 select 字段列表 from 表1 right join 表2 on 条件;

例5:查询dept表所有数据和对应员工信息

select * from emp right join dept on emp.dep_id = dept.id;

或: select * from dept left join emp on emp.dep_id = dept.id;(工作中最常使用left join)

5.子查询

(1)概念:查询中嵌套查询。

子查询分为:单行单列 select 字段列表 from 表 where 字段名 = 子查询;

多行单列 select 字段列表 from 表 where 字段名 in 子查询;

多行多列 select 字段列表 from (子查询)where 条件;

例1:查询班级中成绩高于叶淑华的学生的信息:

select * from stus where score>(select score from stus where name = "叶淑华");

标签:多表,查询,dept,spl,emp,where,id,select
From: https://www.cnblogs.com/mmds/p/16933415.html

相关文章

  • MySQL数据库——多表查询
    MySQL数据库——多表查询一、多表查询的两种方式1.1连表操作'''innerjoin 内连接只连接两张表中公有的数据部分'''select*fromempinnerjoindeponemp.dep_i......
  • 多表查询的两种方法、小知识点补充说明、可视化软件Navicat、多表查询练习题、python
    目录多表查询的两种方法小知识点补充说明可视化软件Navicat多表查询练习题python操作MySQLpymysql补充说明多表查询的两种方法两张表方式1:连表操作 innerjoin 内......
  • 多表查询的方法与第三方模块pymysql
    多表查询的两种方法方式一:连表操作内连接:关键字:innerjoinselect*fromempinnerjoindeponemp.dep_id=dep.id;只连接两张表中公有的数据部分左连接:......
  • SQL查询连续登录用户
    问题:如何判读用户连续5天登录过系统?1.环境MySQL8.0.212.准备测试数据createtablereport_user_login(idbigintauto_incrementprimarykeycomment'主键ID',u......
  • spl增删改查语句
    增:insertinto语句删:delete语句改:update语句查:select语句 语法:一、增加insertinto表名values(值1,值2,...)列如:向表student中插入一条数据:insertintostuden(na......
  • mysql多表查询
    今日内容概要多表查询的两种方法小知识点补充说明可视化软件Navicat多表查询练习题python操作MySQL今日内容详细方式1:连表操作 innerjoin 内连接 selec......
  • 多表查询两种方法、可视化软件navicat、python操作mysql、pymysql模块
    目录多表查询的思路多表查询的两种方法小知识点补充数说明可视化软件Navicat多表查询练习题python操作MySQLpymysql补充说明多表查询的思路表数据准备表数据准备creat......
  • MySQL多表查询语法
    MySQL多表查询语法多表查询准备数据准备createtabledep(idintprimarykeyauto_increment,namevarchar(20));createtableemp(idintprimarykeya......
  • 多表查询 可视化软件Navicat
    今日内容总结多表查询的两种方法#方式一:连表操作 1.innerjoin 内连接select*from表1innerjoin表2on连接部分只连接两张表的共有部分 2.l......
  • 多mysql实例库联合查询
    情况一2个库在同一台物理主机情况二2个库不在同一台物理主机(即2个库分别在不同的物理主机)注意:myemployees库和shoppingCart库在同一台物理主机,如果不在同一台物理......