zoukankan      html  css  js  c++  java
  • 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;

  • 相关阅读:
    Spring Boot中常用的三个注解
    Idea插件
    Java微服务 在 Linux 环境启停Shell脚本
    注解
    Oracle树状结构层级查询
    zabbix安装部署(server部分)
    zabbix监控系统客户端安装
    Windows 用bat脚本带配置启动redis,并用vb脚本使其在后台运行。
    svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted
    PHP网页显示乱码问题总结
  • 原文地址:https://www.cnblogs.com/zhangshan33/p/12303396.html
Copyright © 2011-2022 走看看