1、单表查询 2、模糊查询 3、范围查询 4、聚合函数 5、分组查询

    1. 1、查询所有数据
    2. select * from userinfos
    3. 2、查询表的部分数据
    4. select userid,username,age from userinfos
    5. 3、给列取别名
    6. -- 列名 as 别名 列名 别名 别名=列名
    7. select userid as 用户编号,username 用户名,年龄=age
    8. from userinfos
    9. 4、排序 desc 降序 asc 升序
    10. select useridusernameage from userinfos
    11. order by age asc
    1. --1、% 匹配0个或多个
    2. select * from userinfos where username like '%ad%' --查询所有包含ad
    3. select * from userinfos where username like '%d(a%)' --查询以什么开头或什么结尾
    4. --2_ 匹配单个字符,限制表达式的字符长度
    5. select * from userinfos where like '_dmin' --查询长度为5,后四位为dmin的数据
    6. select * from userinfos where like '_____'
    7. --3、[]范围匹配,括号中字符中的一个
    8. select * from userinfos where like 'ad[mnp]in'
    9. select * from userinfos where like 'ad[m|n|p]in'
    10. select * from userinfos where like 'ad[m-p]in'
    11. --4、[^]不在括号范围之内的单个字符
    12. select * from userinfos where like 'ad[^abc]in'
    1. --select * from test where 子句 条件--给定范围
    2. --比较运算符 > < >= <= <>
    3. select * from userinfos where age>30
    4. --innot in
    5. select * from userinfos where age in(30,23,34)
    6. select * from userinfos
    7. where deptid in
    8. (
    9. select deptid from deptinfos --子查询
    10. )
    11. --between and 等价于 >= and <= betweent and 效率更高
    12. select * from userinfos
    13. where age between 20 and 31 --age>=20 and age<=31
    14. --查询前面多少条,百分比
    15. select top 10 * from userinfos
    16. select 100 percnet * from userinfos
    1. --聚合函数:对一组值执行计算并返回单一的值
    2. count 记录数 计算null 经常与select语句中的group by 结合使用 count(1)比count(*)效率高
    3. --五种聚合函数
    4. count 记录个数
    5. sum 求和
    6. avg 平均值
    7. max 最大值
    8. min 最小值
    9. --count
    10. select count(1) record from userinfos
    11. --sum
    12. select sum(age) from userinfos
    13. --age
    14. select avg(age) from userinfos
    15. --max
    16. select max(age) from userinfos
    17. --min
    18. select min(age) from userinfos
    1. --分组查询
    2. select ...
    3. where ...
    4. group by
    5. --统计各个部门的用户数
    6. select deptid,count(1) 用户数 from userinfos
    7. group by deptid
    8. select deptid,count(1) 用户数 from userinfos
    9. where age>30
    10. group by deptid
    11. having deptid>1 --分组后刷选
    12. order by deptid
    13. --注意:select之后出现的列必须要包含在聚合函数之中或group by之后,
    14. --只针对出现group by 的情况,不然会报错