zoukankan      html  css  js  c++  java
  • 007-数据库之“查”

    四、查

    1、简单查询

    查询姓名和年龄:select name,age from stu 
    查询所有列的数据:select * from stu
    查询性别为男的数据:select * from stu where sex=''

    2、比较运算并查询

    查询小乔的年龄:select age from students where name='小乔'
    查询20岁以下的学生:select * from students where age<20
    查询家乡不在北京的学生:select * from students where hometown<>'北京'

    3、逻辑运算并查询

    查询年龄小于20的女同学:select * from students where age<20 and sex='女'
    查询女学生或'1班'的学生:select * from students where sex='女' or class='1班'
    查询非天津的学生:select * from students where not hometown='天津'

    4、模糊查询

    查询姓孙的学生:select * from students where name like '孙%'
    查询姓孙且名字是一个字的学生:select * from students where name like '孙_'
    查询叫乔的学生:select * from students where name like '%乔'
    查询姓名含白的学生:select * from students where name like '%白%'

    5、范围查询

      查询家乡是北京或上海或广东的学生:

    select * from students where hometown in ('北京','上海','广东')
    select * from students where hometown not in ('北京','上海','广东')

      查询年龄为18至20的学生 between 18 and 20,小值在前:

    select * from students where age between 18 and 20

    6、消除重复数据后返回查询数据

    查询学生的年龄和班级,消除相同的数据
    select distinct age,class from students
    查询学生的所有信息,一个学生只记录一次 select distinct
    * from students

    7、判空

      查询没有填写身份证的学生:

    select * from students where card is null

    8、非空

      判断身份证为空字符

    select * from students where card is not null
    select * from students where card=''

    9、排序:order by

      查询所有学生信息,按年龄从小到大排序:asc:升序(默认升序)

    select * from students order by age asc

      查询所有学生信息,按年龄从大到小排序:desc:降序

    select * from students order by age desc

      查询所有学生信息,按年龄从大到小排序,年龄相同时,再按学号从小到大排序:

    select * from students order by age desc,studentNo

    10、聚合函数

    count,min,max,sum,avg

    查询学生总数:count(card) 不统计为null数据
      select count(*) as 学生总数 from students
      select count(name) from students
      select count(card) from students
    查询女生的最小年龄:select min(age) from students where sex=''
    查询1班的最大年龄:select max(age) from students where class='1班'
    查询北京学生的年龄总和:select sum(age) from students
    查询女生的平均年龄:select avg(age) from students where sex=''

    11、分组:group by(having 分组后过滤)

    查询各种性别的人数:select sex,count(*) from students group by sex
    查询各种年龄的人数:select age,count(*) from students group by age
    查询男生总人数:having后面的条件运算符与where的相同
        select sex,count(*) from students group by sex having sex=''
        select count(*) from students where sex=''

    12、分页:limit

      从start开始,获取count条数据,start索引从0开始:select * from 表名 limit start,count

      每页显示m条数据,求:显示第n页的数据

    select * from students limit (n-1)*m,m

      求总页数:
        查询总条数p1
        使用p1除以m得到p2
        如果整除则p2为总数页
        如果不整除则p2+1为总页数

    注意:
    int(1) 长度没有意思
    varchar(5) 5个字符
    decimal(5,2) 3个整数,2个小数,添加数据超过长度,自动四舍五入
    datetime
    命令行客户端,使用,查看某一个数据类型的使用帮助
    help tinyint

  • 相关阅读:
    Selenium2用最简xpath查找元素
    如何锁定Android系统CPU的频率
    github FATAL:unable to access 'https://github.com/...: Failed to connect to github.com:443; No error
    解压.tar.gz出错gzip: stdin: not in gzip format tar: /Child returned status 1 tar: Error is not recoverable: exiting now
    Shell脚本完成hadoop的集群安装
    Linux压缩与归档
    selenium2-元素管理方式及解析
    selenium2-框架思想介绍
    11.AutoMapper 之值转换器(Value Transformers)
    10.AutoMapper 之自定义值解析器(Custom Value Resolvers)
  • 原文地址:https://www.cnblogs.com/qiuniao/p/11964817.html
Copyright © 2011-2022 走看看