MYSQL语句查询

一、数据库查询语句
    1.select * from table
        通过*查询所有字段,但是不能规定字段的顺序,数据量特别大时查询效率太低,所有建议书写详细的条件查询
    2.where
        添加条件,删除,查询,更新
        eg:
            select * from student where age>30;
            查询年龄大于30岁的所有人
    3.and
        逻辑与关系,在where条件后边添加与逻辑
        eg:
            select * from student where age>30 and age<35;
            查询年龄在30-35之间的人
    4.or
        逻辑或
        eg:
            select * from student where age>30 or age<20;
            查询年龄大于30或小于20的人
    5.distinct
        过滤重复记录,返回唯一结果
        eg:
            select distinct name from student;
            过滤重复的名字,返回唯一的名字
            注:获取的只有name,其他属性不存在,会将重复的名字变为1个
    6.is null
        判断字段是否为空
        eg:
            select * from student where phone is null;
            注:该字段设置属性默认值为空时,如果该字段不设置内容,就是NULL,否则即时不设置内容,系统也不会认为是NULL
    7.is not null
        判断字段是否不为空
        eg:
            select * from student where phone is not null;
    8.if(表达式,v1,v2)
        当表达式为真,返回v1,为假返回v2
        eg:
            select age,if(age>20,'大','小') from student;
            会有一个新字段 if(age>20,'大','小'):大/小
    9.order by
        对查询内容进行排序
    10.order by desc
        对查询内容进行降序排列
        eg:
            select * from student order by age desc
            查询表中所有内容,根据age进行降序排列
    11.order by asc
        对查询内容进行升序排列
        eg:
            select * from student order by age asc
            查询表中所有内容,根据age进行升序排列
    12.rand()
        随机取值
        eg:
            select * from student order by rand() limit 4;
            查询所有数据随机取出4条
    13.limit n
        限制取值n条
        eg:
            select * from student order by age desc limit 4;
            按照降序取出前四条数据
    14.limit m,n
        取出从m开始的n条数据,m是从0开始
        eg:
            select * from student order by age desc limit 7,3;
            按照降序从0开始取,取出3个数据
            用于做分页是非常方便的
    15.between v1 and v2
        在v1与v2之间进行检索内容
        eg:
            select * from student where age between 22 and 60;
            在年龄介于22到60之间查找数据
    16.not between v1 and v2
        检索不在v1与v2之间内容
        eg:
            select * from student where age not between 22 and 60;
            在年龄介于22到60之外查找数据
    17.in(v1,v2,v3....)
        检索索引值是否与小括号中的值相等
        eg:
            select * from student where age in(10,20,33);
            查询所有数据,找出年龄为10,20,33的人
            age=10 or age=20 or age=33
    18.not in(v1,v2,v3....)
        索引值不等于小括号中任何值的数据
        eg:
            select * from student where age not in(10,20,33);
            查询所有数据,找出年龄不为10,20,33的人
            age!=10 and age!=20 and age!=33
    19.like
        匹配字符串
        eg:
            select * from student where name like '李%';
            以李开头的名字被选中
        eg:
            select * from student where name like '%李%';
            包含李的名字被选中
        eg:
            select * from student where name like '%李';
            以李结尾的名字被选中
    20.not like
        like相反的意义
    21.count(*)
        eg:
            select count(*) from student
            统计一共获取多少条数据
    22.max()
        eg:
            select max(age) from student
            获取表中年龄最大的人的年龄值
    23.min()
        eg:
            select min(age) from student
            获取表中年龄最小的人的年龄值
    24.sum()
        eg:
            select sum(age) from student
            获取表中所有人的年龄和
    25.avg()
        eg:
            select avg(age) from student
            获取表中所有人的平均年龄

猜你喜欢

转载自blog.csdn.net/a_alin/article/details/81174262
今日推荐