zoukankan      html  css  js  c++  java
  • mysql基础操作(二):简单查询DQL

    -- 1.查询所有字段
    select * from student;
    -- 2.查询指定的字段
    select id from student;
    select id, name from student;
    -- 3.查询时指定别名
    select id as '编号', name as '姓名' from student; -- as关键字可以省略
    select id '编号', name '姓名' from student; 
    -- 4.查询时添加常量列
    --    需求:查询学生数据时添加一个"班级"列。值为"Java一班"
    --    开始的时候只有id 和 name 这两列;
    --    as的使用:
    --    将id换为编号     将name换为姓名     将Java就业班放入班级中
    --    这里说的查询时添加常量列是指临时添加
    -- 	  并不是像alter table  student add column ...
    --    临时使用的列可以通过查询时动态添加进去
    select id as '编号', name as '姓名', 'java一班' as '班级' from student;
    -- 5.查询时合并列
    --    需求:查询每个学生的总分。
    --    表如下:
    --    id    name   age   servlet   mysql
    --    1     张三   20    75        80
    --    2     李四   19    86        90
    select name as '姓名', (servlet+mysql) as '总成绩' from student;
    -- 注意:合并列的字段必须是数值类型的字段。如果是非字符的字段合并查询没有意义。
    如果要进行字段合并(如英文名的first name 和 last name 要使用concat函数)
    -- 例如:
    CONCAT(staff.first_name, CONCAT(" ", staff.`last_name`))
    -- 6.查询去除重复记录(distinct)
    --    需求:查询出有哪些地区的学生(排除重复)
    --    表如下:
    --    id    name   age   servlet    mysql   address
    --    1     张三   20    75         80      成都锦江
    --    2     李四   18    86         90      成都高新
    --    3     王五   19    85         66      成都武侯
    select distinct address from student; -- 查询出address中的不重复的信息。 
    select distinct(address) from student; -- 查询出address中的不重复的信息。 
    -- 以上两种语法都是正确的:
    -- 1.第一个中distinct是关键字。
    -- 2.第二个中的distinct()是函数。
    -- 7.条件查询(where)

    -- 7.1 逻辑条件: and(与)   or(或)
    -- 当出现两个或两个条件以上的时候,那么这两个条件一定会存在逻辑关系。
    -- 当出现逻辑关系的时候,要么是与关系,要么是或关系(只存在这两种关系)
    -- 需求:查询学生的id为1,且姓名为张三的学生
    select * from student where id=1 and name = '张三'; -- (交集)
    -- 需求: 查询学生的id为2,或者姓名为张三的学生
    select * from student where id = 1 or name = '张三'; --(并集)
    

    -- 7.2 比较条件:
    -- >   <   >=  <=   =(和Java中的 == 相同 都是判等)  <>(mysql中的不等于 != )
    -- (between and)(判断条件)
    -- 需求:查询servlet分数大于80分的学生
    select * from student where servlet>80;
    -- 需求:查询mysql分数小于或者等于85分的学生
    select * from student where mysql<=85;
    select * from student where mysql<85 or mysql=85;
    -- 需求:查询servlet分数大于或等于80分,且小于或等于85分的学生
    select * from student where servlet>=80 and servlet<=85;
    select * from student where servlet between 80 and 85; -- 80和85都包括在内
    -- between and  在 ...之间,两边的都包括
    -- 需求:查询年龄不等于30岁的学生
    select * from student where age<>30;  -- !=不是标准用法,mysql数据库中用<>

    -- 7.3 判空条件:is null, is not null, =''(等于空串), <>''(不等于空串)
    -- null:表示没有数据  is null、 is not null
    -- 空字符:有数据  
    -- 需求:查询没有性别数据的学生(包括null和空串)
    select * from student where gender is null or gender='';
    -- 需求:查询有性别数据的学生
    select * from student where gender is not null and gender <> '';
    

    -- 7.4 模糊条件:like

    -- 模糊替代符号:
    -- %:替代任意个字符0~n个都可以
    -- _:下划线只能替代一个字符
    -- 需求:查询姓'李'的学生
    select * from student where name like '李%';
    -- 需求:查询名字中包含'四'字的学生
    select * from student where name like '%四%';
    -- 需求:查询姓'李',全名只有两个字的学生
    select * from student where name like '李_';
    -- 需求:查询姓'李',全名只有三个字的学生
    select * from student where name like '李__';

    -- 8.聚合查询:用于统计结果
    -- max()   min()   avg()   count() 
    -- 需求:查询servlet的最高分
    select max(servlet) from student; 
    -- 需求:查询mysql的最低分
    select min(mysql) from student;
    
    -- 需求:查询servlet中的平均分
    select avg(servlet) from student;
    -- 需求:统计当前有几个学生
    select count(*) from student; --使用count去统计数据,不要使用存在null的数据
    -- count(id):统计有值的id字段的数量(排除掉null的数据)
    select count(id) from student;
    

    -- 9.分页查询(limit)
    -- 查询的起始行数,查询的行数
    -- 需求:查询第1,2条数据
    select * from student limit 0,2;
    
    -- 需求:查询第3,4条数据
    select * from student limit 2,2;
    
    -- 需求:查询第5,6条数据
    select * from student limit 4,2;
    
    -- 需求:学生有20条数据,每页显示5条,共4页
    -- 现在要查看第三页(第11条到第15条)的学生数据sql:
    select * from student limit 10,5;
    -- 现在要查看第四页(第16条到第20条)的学生数据sql:
    select * from student limit 15,5; 
    
    -- 已知:当前页码,每页显示条数
    -- 结论:分页查询当前页数据的sql:
    -- select * from student (当前页码-1)*每页显示条数,每页显示条数;


    -- 10.查询后排序(order by)

    -- desc:降序。数值从大到小,字母z-a
    -- asc:升序。数值从小到大,字母a-z
    
    select * from student; -- 默认情况下是按照插入顺序进行的排序
    -- 需求:按照id的升序排序
    select * from student order by id asc; 
    select * from student order by id;
    -- 这里的asc可以省略,默认的排序方式就是升序排序;
    -- 需求:按照servlet成绩降序排序
    select * from student order by servlet desc;
    
    -- 需求:先按照age升序,再按照servlet成绩降序排序;
    select * from student order by age asc, servlet desc;
    -- 这里就以age为主,age升序排完了之后,在不影响age的情况下
    -- 对servlet进行降序排序;(当存在年龄相同的时候)
    -- 多个条件排序:先按照前面的条件排序,当出现重复记录,再按照后面的条件排序
    
    select * from student order by name asc;-- 按照姓名升序排列
    -- 最好是英文,utf8对汉字的排码不能支持按照汉字首字母排序
    -- 如果要按照名字排序的话,首先要使用gbk编码

    -- 11.分组查询(group by)  先查询进行条件判断,再进行分组

    -- 需求:查询每个地区有多少人
    -- 预期结果:
    -- 成都锦江  3
    -- 成都高新  1
    -- 1) 对地区进行分组
    -- 2) 在分组的基础上进行统计,统计的是每一组的数据
    select address, count(*) from student group by address;
    -- 需求:统计男女的人数
    -- 注意:where条件应该 在group by 之前
    select gender, count(*) from student where gender is not null and gender<>'' group by gender;
    -- 需求:统计男同学中成绩最高的
    select gender, max(servlet) from student where gender is not null and gender<>'' group by gender;


    -- 12.分组查询后筛选  先进行分组,分组之后再进行条件判断
    -- 需求: 查询哪些地区的人数是大于2个的地区
    -- 1)查询每个地方有多少人。
    -- 2)再对每个地区人数进行判断,人数大于2的地区即为所求 
    -- 这里涉及关键字 having having特定使用指:对分组之后的结果进行筛选
    
    select address, count(*) from student group by address having count(*)>2;
    -- 注意:having是用在group by 之后的 ; where是用在 group by 分组之前的
  • 相关阅读:
    Python中的类(上)
    Django REST Framework API Guide 07
    Django REST Framework API Guide 06
    Django REST Framework API Guide 05
    Django REST Framework API Guide 04
    Django REST Framework API Guide 03
    Django REST Framework API Guide 02
    Django REST Framework API Guide 01
    Django 详解 信号Signal
    Django 详解 中间件Middleware
  • 原文地址:https://www.cnblogs.com/mzywucai/p/11053500.html
Copyright © 2011-2022 走看看