• mysql学习笔记--数据库单表查询


    一、查询语句

      1.  select [选项] 列名 [from 表名]  [where 条件]  [order by 排序]  [group by 分组]  [having 条件]  [limit 限制]

      2. 字段表达式

        select 10*10

        select '锄禾日当午'

      3. 通过as给列名取别名,as可以省略 

        select 10*10 as result

        select 10*10 result

      4. from子句

        a. from:来自,from后面跟的是数据源。数据源可以有多个。返回笛卡尔积。

      5. dual伪表

        dual是一个伪表。在有些特殊情况下,没有具体的表可以参与,但是为了保证select语句的完整又必须要一个表名,这时候就要用伪表。

        dual表示为了保证select语句的完整性。

      6. where子句

        a. where后面跟的是条件,在数据源中进行筛选。条件为真,就 返回当前记录。

        b. mysql支持的运算符

          大于:>  小于:<  大于等于:>=  小于等于:<=  等于:=  不等于:!=  与:and  或:or  非:not

      7. in | not in

        a. 查找北京和上海的学生

          select * from stu where stuAddr='北京' or stuAddr='上海'

          select * from stu where stuAddr in ('北京','上海') 

        b. 查找不是北京和上海的学生

          select * from stu where stuAddr not in ('北京','上海')

      8. between ... and | not between ... and 查找某个范围的记录 

        a. 查找年龄在18到20之间的学生

          select * from stu where stuAge >=18 and stuAge <=20

          select * from stu where stuAge between 18 and 20

        b. 查找年龄不在18到20之间的学生

          select * from stu where stuAge <18 or stuAge >20

          select * from stu where not (stuAge >=18 or stuAge <=20)

          select * from stu where stuAge not between 18 and 20  

      9. is null | is not null

        a. 查找ch为null或者math为null的学生

          select * from stu where ch is null or math is null

        b. 查找ch和math都不为null的学生

          select *from stu where ch is not null and math is not null

      10. 聚合函数

        a. sum() :求和

          select sum(ch) from stu; 求语文成绩总分

        b. avg():平均数

        c. max():最大值

        d. min():最小值

        e. count():记录数

          select sum(ch) '语文总分', avg(ch) '语文平均分', max(ch) '语文最高分', min(ch) '语文最低分', count(*) '总记录数' from stu;

      11. 通配符

        a. _ [下划线] 表示任意一个字符

        b. % 表示任意字符

      12. 模糊查询(like)

         select * from stu where stuName like '张%'

      13. order by 排序

        a. asc 升序【默认】

        b. desc 降序

          select * from stu order by ch desc;语文成绩降序排列

          select * from stu order by ch; 默认升序排列

        c. 多列排序

          select *,(ch+math) as '总分' from stu order by stuAge asc, (ch+math) desc;

      14. group by 分组查询(将查询的结果分组,分组查询的目的在于统计数据)

        a. select avg(stuage) as '平均年龄',stusex from stu group by stusex;

        b. 注意:如果是分组查询,查询字段必须是分组字段和聚合函数,如果查询字段是普通字段,只取分组的第一个值。

        c. select group_concat(stuname),stusex from stu group by stusex 将同一组的值连接起来显示

        d. 注意:分组默认为升序排序,可以加desc改为降序。select * from stu group by stuage desc;  

        e. 多列分组:select stuaddr,stusex,avg(stuage) from stu group by stuaddr,stusex;

      15. having条件:在结果集上继续进行筛选

        a. select stusex,count(*) total from stu group by stusex having totalz >=5;

      16. limit

        a. 语法:limit起始位置,显示长度

          select * from stu limit 0,2; (从0开始取2条)

        b. 起始位置可以省略,默认从0开始

        c. 例子:班级总分前三名 select *,(ch+math) total from stu order by total desc limit 3;

          前三个地址改成上海:update stu set stuaddr='上海' where name is not null limit 3;

      17. 选项

        a. all:显示所有数据【默认】

        b. distinct:去除结果集中重复的数据

        

        

        

  • 相关阅读:
    沿着河边走,环城60公里
    与客家土楼的约会(叶剑英故居福建)
    与客家土楼的约会(增城河源)
    与客家土楼的约会(东源龙川)
    C#几个经常犯错误
    C# 获取当前路径方法
    C# Stopwatch与TimeSpan详解
    话说程序员成熟的标志
    C#开源项目介绍
    C#各种加密算法的研究
  • 原文地址:https://www.cnblogs.com/DjanFey/p/10678641.html
Copyright © 2020-2023  润新知