• MySQL子查询


    子查询:内查询、嵌套查询

    select 。。。。。。(select)

    括号里面的查询语句会先于主查询语句执行,然后再把子查询的结果作为条件返回给主查询条件进行过滤

    报错:子查询返回的结果只能是一列,多列就报错

    where条件in什么,子查询的列就是什么,两者之间应该一一对应,否则识别不到

    这两张表有相同的名字,查询成绩

    取反。这个表中大于76分的都不要,只保留低于76分的

    子查询语句还可以用在insert、update、delete

    insert into student select * from info where id in (select id from info where sex='女');

    insert into test1 select * from info where id in (select id from info where address like '%四川%');

    update info set score=50 where id in (select id from student where id = 2);

    update info set score=100 where id not in (select id from test1 where id > 1);

    delete from info where id in (select id where score > 75);

    exists:关键字子查询。主要用于判断子查询的结果是否为空。不为空,返回true。为空则为false

    视图:MySQL中的视图view

    视图在MySQL中是一个虚拟的表。基于查询结果得出的一个虚拟表。

    在工作中,我们查询的表未必是真表。有可能是基于真表查询结果的一个虚拟表

    可以简化负载的查询语句,隐藏表的细节,提供安全的数据访问

    创建视图表可以是一张表的结果集,也可以是多个表共同的查询的结果集

    create view test2 as select * from info where score >= 80;

    select * from test2;

    desc test2;

    desc info;

    视图表和真表之间的区别:

    1、存储方式不一样。

    真表存储实际数据,真正写在磁盘当中的

    视图表不存储任何数据,仅仅是一个查询结果集的虚拟表

    2、数据更新不一样

    真表可以增删改查

    视图表一般只能用于查,展示数据

    3、占用空间不一样

    真表真实占用空间

    视图表不占用数据库空间的

    查看当前库有哪些视图表,并删除

    create table tset01 (

    id int,

    name varchar(15),

    age int(3)

    );

    根据info的id,name score,加上test01的age?

    create view v_info(id,name,score,age) as select a.id,a.name,a.score,b.age from info a.tset01 b where a.name=b.name;

    select * from v_info;

    update info set score=90 where name='胡晋';

    select * from v_info;

    源表的数据发生变化,视图表的数据同步更新

    update v_info set age=9000 where name='王宇航';

    修改了视图表,源表的数据也会发生变化。一般情况我们是不对视图进行改的操作

    真表占了80%,视图适用于安全性要求比较高的场景。对外访问基本都是视图

    null值和空值

    null值就是社么都没有,类似于真空

    空值可以理解为空气

    如何鉴别

    select * from info where score is null;

    select * from info where score is not null;

    select * from info where address is not null;

    为空,不是null值

    null值不被统计,但是空值可以被统计

    连接查询

    1、内连接

    把两张表或者多张表(不超过三张)之间同时符合特定条件的数据记录的组合。多个表之间有一个或者多个列的相同值才会有查询结果。

    select a.id,a.name from tset01 a inner join info b on a.name=b.name;

    select a.id,a.name from tset01 a inner join info b on a.id=b.id;

    select a.id,a.name from tset01 a inner join info b on a.name=b.name and a.id=b.id;

    2、左连接

    又叫左外连接,在语句中用left join关键字来表示。在左连接中,左侧表是基础表,接收左边的所有行,然后和右表(参考表)记录进行匹配。匹配左表中的所有行以及右表中符合条件的行

    select * from tset01 a left join info b on a.name=b.name;

    select a.id,a.name,b.score,b.name from tset01 a left join info b on a.name=b.name;

    3、右连接

    又叫右外连接,right join关键字连接。以右侧表为基础,接收右侧表的所有记录,匹配的记录,不匹配的记录null值

    select a.id,a.name,b.score,b.name from tset01 a right join info b on a.name=b.name;

    select * from tset01 a right join info b on a.name=b.name;

    select * from tset01 a right join info b on a.id=b.id;

    select * from tset01 a right join info b on a.id=b.id and a.name=b.name;

  • 相关阅读:
    涂鸦Wi-Fi&BLE SoC开发幻彩灯带
    数字IC验证面试常见的问题汇总,你掌握了几个?
    1700. 无法吃午餐的学生数量
    Go-Python-Java-C-LeetCode高分解法-第八周合集
    全网最详细SpringBoot、SpringCloud整合阿里云OSS对象存储服务
    智能多价格-面向电商的全栈开发利器
    这个队列的思路是真的好,现在它是我简历上的亮点了。
    Git存储原理——Git对象
    电力巡检/电力抢修行业解决方案:AI+视频技术助力解决巡检监管难题
    Thymeleaf里面将字符串和变量拼接
  • 原文地址:https://blog.csdn.net/a91888888/article/details/134292748