Mysql多表设计

前言

多表查询中要给每一表起别名

  • tableA as 别名1 , tableB as 别名2 ; (111111推荐)

  • tableA 别名1 , tableB 别名2 ;

例子:

复制代码
select emp.name , dept.name
from tb_emp emp inner join tb_dept dept
on emp.dept_id = dept.id;

一对一

在任意一方加入对方表的主键

(一对多的一中)

一对多

连接查询

  • 在(t1)多的一方加入 一(t2)的主键

    例:select * from t1,t2 where t1.t2_id=t2.id;

内连接 查询A,B交集部分数据

复制代码
##### 隐式内连接

*

      select tb_emp.name , tb_dept.name -- 分别查询两张表中的数据
      from tb_emp , tb_dept -- 关联两张表
      where tb_emp.dept_id = tb_dept.id; -- 消除笛卡尔积
复制代码
##### 显示内连接

*

      select tb_emp.name , tb_dept.name
      from tb_emp inner join tb_dept
      on tb_emp.dept_id = tb_dept.id;

外连接(左右连接可以切换)

复制代码
##### 左外连接(推荐)

* 左外连接相当于查询表1(左表)的所有数据,当然也包含表1和表2交集部分的数据。即使表2中没有与之对应的数据该数据也会被查询出来
* select 字段列表 from 表1 left \[ outer \] join 表2 on 连接条件 ... ;
*

      -- 左外连接:以left join关键字左边的表为主表,查询主表中所有数据,
      以及和主表匹配的右边表中的数据
      select emp.name , dept.name
      from tb_emp AS emp left join tb_dept AS dept 
           on emp.dept_id = dept.id;
复制代码
##### 右外连接

* select 字段列表 from 表1 right \[ outer \] join 表2 on 连接条件 ... ;

子查询

  • SQL语句中嵌套select语句,称为嵌套查询,又称子查询。
复制代码
    SELECT * FROM t1 WHERE column1=( SELECT column1 FROM t2 ... );

*

  ##### 标量子查询

  * 子查询返回的结果是单个值(数字、字符串、日期等),最简单的形式,这种子查询称为标量子查询。

  * 常用的操作符: = \<\> \> \>= \< \<

  *

        ​
        -- 1.查询"教研部"部门ID
        select id from tb_dept where name = '教研部';    #查询结果:2
        -- 2.根据"教研部"部门ID, 查询员工信息
        select * from tb_emp where dept_id = 2;

        -- 合并出上两条SQL语句
        select * from tb_emp where dept_id = (select id from tb_dept where name = '教研部');

        ​

*

  ##### 列子查询

  * (子查询结果为一列,但可以是多行)

  *

    | **操作符** |     **描述**     |
    |---------|----------------|
    | IN      | 在指定的集合范围之内,多选一 |
    | NOT IN  | 不在指定的集合范围之内    |

  *

        -- 1.查询"销售部"和"市场部"的部门ID
        select id from tb_dept where name = '教研部' or name = '咨询部';    #查询结果:3,2
        -- 2.根据部门ID, 查询员工信息
        select * from tb_emp where dept_id in (3,2);

        -- 合并以上两条SQL语句
        select * from tb_emp where dept_id in (select id from tb_dept where name = '教研部' or name = '咨询部');

*

  ##### 行子查询(子查询结果为一行,但可以是多列)

*

  ##### 表子查询(子查询结果为多行多列\[相当于子查询结果是一张表\])

  *

        -- 1.查询"方东白"的入职日期
        select entrydate from tb_emp where name = '方东白';     #查询结果:2012-11-01
        -- 2.查询指定入职日期之后入职的员工信息
        select * from tb_emp where entrydate > '2012-11-01';

        -- 合并以上两条SQL语句
        select * from tb_emp where entrydate > (select entrydate from tb_emp where name = '方东白');
  • 子查询可以书写的位置
    • where之后

    • from之后

    • select之后

相关推荐
北友舰长44 分钟前
基于Springboot+thymeleaf图书管理系统的设计与实现【Java毕业设计·安装调试·代码讲解】
java·spring boot·mysql·课程设计·图书管理·b/s·图书
云和恩墨2 小时前
OceanBase企业版会话级SQL跟踪实操:DBMS_MONITOR(类Oracle 10046事件)
数据库·sql·oracle·oceanbase
为什么不问问神奇的海螺呢丶2 小时前
oracle 数据库巡检 sql
数据库·sql·oracle
麦麦鸡腿堡2 小时前
MySQL数据库操作指令
数据库·mysql
陈天伟教授8 小时前
人工智能训练师认证教程(2)Python os入门教程
前端·数据库·python
Elastic 中国社区官方博客9 小时前
Elasticsearch:在分析过程中对数字进行标准化
大数据·数据库·elasticsearch·搜索引擎·全文检索
聪明努力的积极向上9 小时前
【MYSQL】字符串拼接和参数化sql语句区别
数据库·sql·mysql
代码or搬砖9 小时前
RBAC(权限认证)小例子
java·数据库·spring boot
神仙别闹9 小时前
基于QT(C++)实现学本科教务系统(URP系统)
数据库·c++·qt
2301_768350239 小时前
MySQL为什么选择InnoDB作为存储引擎
java·数据库·mysql