zoukankan      html  css  js  c++  java
  • mysql 数据库学习第二天

    查询数据(单表查询)

    查询所有列   select  * from  表名;

    查询指定列   select  字段,字段,字段 from  表名;

    查询时添加常量列   select  '新字段' as '常量名' from  表名;

    查询时合并列     select  (字段1+字段2)as '常量名' from  表名;注:合并的只能时相同类型的字段

    查询时去重复记录   select distinct  字段  from 表名;

                                   select distinct(字段)from 表名;

    条件查询    

            逻辑条件  and   or

            比较条件   >  <  >=   <=  <>  between 数值1  and  数值2

            判空条件    is  null  /  is not  null  /   ='  '   / <>'  '

             如:查询地址为空的学生(包括null和空字符串)

              null  vs   空字符串

              null    表示没有值

              空字符串   表示有值!

             如:判断null

              select * from 表名   where 字段 is null

     模糊查询   like

           %:表示任意个字符

           _: 表示一个字符

          例如:查询姓张的学生    select * from  表名  where  like ‘张%’;

                     查询姓李的  且名字是两个字的   select  * from 表名   where  like ‘李_’;

     聚合查询   sum()  avg()   max()   min()  count()

           select  sum(score) as 'score的总分' from  表名;

           select  avg(score) as 'score的平均分' from  表名;

           select  max(score) as 'score的最高分' from  表名;

           select  min(score) as 'score的最低分' from  表名;

           select  count(*)  from  表名;  查询总数

           注:count()函数统计的数量不包含null的数据

    分页查询   limit  起始行,查询几行

           总结的小规律:分页查询的数据的sql: select * from 表名 limit (当前页-1)*每页显示多少条,每页显示多少条

           如:第一页    查询第1,2条数据

             select * from  表名 limit 0,2;

                 第二页  查询第3,4条数据

              select  * from 表名  limit 2,2;

    查询排序  (order by)

          语法:order by  字段   asc/desc

          asc: 顺序,正序,递增

          desc: 倒序,逆序,递减

    分组查询  (group   by)

           如:查询男女的人数   (先分组,再统计人数)

           select  gender,count(* ) from student  group by gender;

    分组后筛选  (having)

           如:查询总人数大于3的性别  (先分组   再统计  最后加查询条件 ) 

           select  gender,count(* ) from student  group by gender  having count(*)>3;

  • 相关阅读:
    第八场 hdu 6136 Death Podracing(dfs+思维)
    第九场 hdu 6162 Ch’s gift(树链剖分+线段树)
    树链剖分 模板
    CodeForces
    第十场 hdu 6172 Array Challenge(矩阵快速幂)
    第十场 hdu 6171 Admiral(双向bfs找交点)
    CodeForces
    第十场 hdu 6178 (bfs)
    第十场 hdu 6180 Schedule (multiset)/(思维)
    第八场 hdu 6143 Killer Names(思维题)
  • 原文地址:https://www.cnblogs.com/Java-125/p/8558883.html
Copyright © 2011-2022 走看看