• MySQL-查询SQL语句


    1.基础查询

    范围查询

    在范围in
    select 字段 from 表名 where 字段 in (值,值);
    select id from stu where id in (2,3);

    不在范围not in
    select 字段 from 表名 where 字段 in (值,值);
    select id from stu where id not in (2,3);

    在之间between
    select 字段 from 表名 where 字段 between 值 and 值;
    select id from stu where id between 1 and 3;

    排序order by asc

    select 字段 from 表名 order by 字段;(升序)
    select * from stu order by id;

    降序order by desc

    select 字段 from 表名 order by 字段 desc;(降序)
    select * from stu order by id;

    去重distinct

    select distinct(字段) from 表名;
    select distinct(id) from stu;

    限制limit

    select 字段 from 表名 limit 数字;
    select * from stu limit 2;

    分组group by

    select 查询字段 from 表名 where 查询条件 group by 查询字段;
    select id from stu where id >1 group by id;

    分组过滤having

    select 查询字段 from 表名 group by 查询字段 having 过滤条件;
    select id from stu group by id having id > 1;

    2.高级多表查询

    再新建一个表
    create table class (id int,user_id int,name char(16));(user_id与stu表中的id值一致)

    内连接inner join on

    select 字段 from 左表名 新表名 inner join 右表名 新表名 on 左表名.字段 = 右表名.字段(两个表中字段值一致)
    select * from stu u inner join class s on u.id = s.user_id;

    左连接left join on

    select 字段 from 左表名 新表名 inner join 右表名 新表名 on 左表名.字段 = 右表名.字段(两个表中字段值一致)
    select * from stu u left join class s on u.id = s.user_id;

    右连接right join on

    select 字段 from 左表名 新表名 right join 右表名 新表名 on 左表名.字段 = 右表名.字段(两个表中字段值一致)
    select * from stu u right join class s on u.id = s.user_id;

    嵌套查询select(select)

    select 字段 from 表名 where 字段 条件 (select 字段 from 表名 where 字段);(将select中的语句看成一个新表)
    select names from stu where id in (select user_id from class where name='一年级');

    合并并去重union

    将多个结果合并到一个表,并删除多个重复数据
    select 字段 from 表名 union select 字段 from 表名;
    select id from stu union select user_id from class;

    合并不去重union all

    将多个结果合并到一个表,并删除多个重复数据
    select 字段 from 表名 union all select 字段 from 表名;
    select id from stu union all select user_id from class;

  • 相关阅读:
    [Leetcode]480. Sliding Window Median
    C++的一些小的知识点
    extern关键字
    c++的默认构造函数 VS 深拷贝(值拷贝) 与 浅拷贝(位拷贝)
    inline-内联函数的优点以及与宏定义的区别
    char类型输出地址
    c++ 对象的内存布局
    Shell 去掉文本中的空格
    牛客网-网易编程题 双端队列找规律
    计算机网络概观
  • 原文地址:https://www.cnblogs.com/zhangshan33/p/12303396.html
Copyright © 2020-2023  润新知