• mysql常用语句之DQL:查询语句


    DQL(查询语句)

    常用关键字:

    关键字说明
    or
    and
    having分组后的附加条件
    group by分组
    order by排序:asc:正序,desc:倒序
    in存在于某个值中
    not in不存在与某个值中
    inner join … on链接多表
    left join … on左外连接
    right join … on右外连接
    left(right,inner) out join … on去重
    count计数
    AVG平均值
    sum求和
    max最大值
    min最小值
    like ‘关键字%’取含有关键字的值
    distinct去重
    round四舍五入
    where 1=1全选
    where 1=2全不选
    limit 1,5从第二行开始显示5条数据
    top 10 : select top 10 * from 表名显示前十条数据
    newid()随机

    1.选择查询:
    select * from 表名 where 范围

    2.完全查询:
    select * from 表名

    3.模糊查询:
    select * from 表名 where 列名 like ‘%value%’(查询包含value的值)

    4.排序查询(倒序,默认为正序):
    select * from 表名 order by 列名 desc

    5.计数查询:
    select count as 别名 from 表名

    6.求和查询:
    select sum(field) as 别名 from 表名

    7.平均值查询:
    select avg(filed) as 别名 from 表名

    8.最大值查询:
    select max(filed) as 别名 from 表名

    9.最小值查询:
    select min(filed) as 别名 from 表名

    10.四舍五入查询:
    select round(min(filed),要保留的小数位数) as 别名 from 表名

    11.去重查询:
    select distinct 列名 from 表名

    12.分组查询:
    select * from 表名 group by 列名

    13.多条件查询:
    select * from 表名 where 列名=‘范围’ and 列名=‘范围’

    14.不确定条件查询:
    select * from 表名 where 列名=‘范围’ or 列名=‘范围’

    15.多条件分组求值查询:
    select 列名,min(filed)from 表名 where 列名=‘范围’ group by 列名 having sum(列名)

    16.子查询(子查询的结果作为主查询的条件):
    select * from 表名 where=(select id from 表名 where 列名=‘范围’)

    17.连表查询(内连接):
    select * from 表名 inner join 表名 on 主键=外键(必须有主外键)

    18.连表查询(内连接)(去重):
    select * from 表名 inner out join 表名 on 主键=外键(必须有主外键)

    注:内连接只显示相关联的数据
    19.连表查询(左外连接)(显示左表的全部信息和右表相关联的信息):
    select * from 表名 left join 表名 on 主键=外键(必须有主外键)

    20.连表查询(右外连接)(显示右表的全部信息和左表相关联的信息):
    select * from 表名 right join 表名 on 主键=外键(必须有主外键)

    21.连表查询(全外连接)(显示两个表中的所有记录):
    select * from 表名 full/cross join 表名 on 主键=外键(必须有主外键)

    22.说明:几个高级查询运算词
    A:union(结合两个结果表并消除其中的重复行而派生出另一个表)
    例:select name from 表名1 union select name from 表名2(去除name的重复行)
    B:except(通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表)
    C:intersect(通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表)
    注:当这三个运算符与all一起使用时不消除重复行

    三、DML(数据操作语句:插入、修改、删除)

    1.数据插入
    完全插入:insert into 表名 values(值1,值2,值3,…)
    选择插入:insert into 表名(列名1,列名2,列名,3…) values(值1,值2,值3,…)
    批量插入:insert into 表名 values(值1,值2,值3,…),(值1,值2,值3,…),(值1,值2,值3,…)…
    注1:完全插入可以省略列名,但是值必须包含所有字段,批量插入类同
    注2:选择插入必须选择需要插入的字段,选择对应字段的值,批量插入类同

    2.修改语句
    update 表名 set 列名=‘值’ where 列名=‘范围’
    where 1=1(全选)
    where 1=2(全不选)
    注:不加where条件为修改所有指定列的值

    3.删除语句
    delete from 表名 where 列名=‘范围’
    注:不加where条件为删除所有的值

  • 相关阅读:
    大数据高级开发工程师——大数据相关工具之四 Superset
    使用 OpenCV 进行 YOLO 对象检测
    VueRouter 源码解析
    【学习总结】什么是弹性负载均衡? LB和ELB的区别
    【论文阅读 07】Anomaly region detection and localization in metal surface inspection
    Cloudpods容器化经验分享
    dreamweaver网页设计作业制作 学生个人网页单页 WEB静态网页作业模板 大学生个人主页博客网页代码 dw个人网页作业成品简单页面
    网络代理技术的护航与网络安全
    共享内存 - 多进程编程(三)
    NFT Insider #74:AnimocaBrands 完成 1.1 亿美元融资,YGGSEA社区举办中秋节NFT赠送活动
  • 原文地址:https://blog.csdn.net/qq_15125937/article/details/125499202