DQL

作者: 博行天下 | 来源:发表于2017-03-23 21:53 被阅读0次

    SELECT *FROM BB;基础查询语句
    插入字段可以不传值的情况:
    1、自增的字段可以不传值;
    2、有默认值;
    3、可为空的字段可以不传值;

    指定字段查询:
    select name,age,sex from bb;
    select province from bb;
    select distinct province from bb;把重复的去掉
    select name from bb where age > 56;查询年龄大于56的人名是什么
    select *from bb where age > 34;
    select name from bb where age > and money =4000;
    select *from bb where age between 1 and 5; 包括1和5;在闭区间年龄是1和5内的数据
    between and 闭区间
    in/not in 在/不在指定的集合内
    select *from bb where age in(1,3,5,7);年龄是1,3,5,7的数据
    select *from bb where age not in(1,3,5,7);年龄是1,3,5,7的数据
    like 模糊查询
    select *from bb where name like  '_玲';把两个字中第二个字是玲的查询出来,其中_代表任意一个字符
    select *from bb where name like '%玲';只要最后一个字是“玲”就行,不管”玲“前面几个字。%代表:匹配“玲”前面所有字符;
    select *from bb where name like '林%';查询只要是“林”开头的就行,不管“林”后面几个字
    排序:
    select *from bb order by money;默认是从低到高排序;
    select *from bb order by money desc;  desc: 降序
    多字段排序
    select *from bb order by money desc,age desc;如果金钱相等,就按照年龄降序排序;
    asc:升序
    多字段排序用逗号隔开
    limit:限制,limit 偏移量,数量;
    limit 2,3;从第二个开始显示3个数据;
    limit 0,3;
    select *from bb order by money desc limit 3;只显示前三个
    
    select max(money) from bb;
    min()
    avg()平均
    count()多少个
    sum()总数
    max()
    

    分组

    select province,count(province) from bb group by province;根据省份分组显示省份和省份的个数;
    可以起别名:as 可以省略
    select province as pro,count(province) as count from bb group by province;
    having:对结果进行限制,where对表限制,having对组限制
    select province as pro,count(province) as count from bb group by province having count > 2;
    select province as pro,count(province) as count from bb group by province having count > 2 order by desc limit 1,1;
    
    

    implode(join)将一个数组按照什么进行分割
    explode把一个字符串分割成数组

    相关文章

      网友评论

          本文标题:DQL

          本文链接:https://www.haomeiwen.com/subject/zdatottx.html