zoukankan      html  css  js  c++  java
  • 数据库操作

    1. 数据的操作

        • 清空表

          • delete from 表;
            • 清空表,但不会清空自增字段的offset(偏移量)值
          • truncate table 表:
            • 会清空表和自增字段的偏移量
        • 删除某一条数据

          • delete from 表 where 条件;
        • update 表 set 字段= 值 where 条件
        • 单表查询

          • Select * from 表名;

          • select 字段1,字段2,。。。。 from 表名;

          • select 字段 as 新名字。。。from 表名;、

          • 去重:

            • select Distinct 字段 from 表名;
            • select distinct 字段1, 字段2 from 表名。
          • 四则运算:

            • select 字段*12 as 字段别名 from 表名;
          • 定义显示格式: concat(字段1,字段2)

            • select concat(字段1,字段2) from 表名;
            • select concat_ws('用什么拼接',字段1,字段2) from 表名;
          • case

            select (
            case
            when 字段 == 'xxx' then
            		字段
            when 字段 == 'xxxx' then
            		concat(字段,'sb')
            else
            		concat(字段,'dasb')
            end
            ) as 新字段名
            from 表名;
            
          • where 筛选所有符合条件的行

            • 比较运算:> ,<,=,>=,<=,!=;
            • 范围
              • between … and …
              • in (10000,20000) 只要10000或者20000
            • like
              • % :通配符 select * from employee where emp_name like 'j%';
              • _ : 通配符select * from employee where emp_name like '程__';
            • regexp
              • '^a'
              • 'g$'
            • 逻辑运算
              • not
              • and
              • or
        • 多表查询

      • Group by:根据谁分组,可以求这个组的总人数,最大值,最小值,。。,但是求出来的值只是和分组字段对应。并不和其它任何字段对应,这个时候查出来的所有其他字段都不生效。

        •   注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数
          
        • select age,count(id) from employee group by age;

      • 聚合函数

        • count
        • max
        • min
        • sum
        • avg
    2. Having 过滤语句:

      • 在having条件中可以使用聚合函数,在where中不行

      • 适合去筛选符合某一条件的某一组数据,而不是某一行数据

      • 先分组再过滤:求平均薪资大于xx的部门,求人数大于xx的性别,求大于xx人的年龄段

        #!!!执行优先级从高到低:where > group by > having 
        #1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。
        #2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数
        
    3. order by:排序 末尾+desc 改排序方式

      • 默认升序 asc(从小到大)
      • desc :降序
      • asc :升序
      • order by age,salary desc
        • 优先根据age从小到大排,在age相同,再根据薪资从小到大排
        • select * from employee order by salary;
    4. limit

      • limit m,n 从第m+1项 取n项,不写m就默认0.
      • limit n offset m
  • 相关阅读:
    Spring 学习笔记
    Java Web整合开发(33) -- SSH和SSJ
    2、常用操作
    jsonp使用
    PHP curl 封装 GET及POST方法很不错的
    浅谈CSRF攻击方式 转
    谷歌插件请求ci 解决CI框架的Disallowed Key Characters错误提示
    phpstorm10.0.3 下载与激活
    Mysql全文搜索match against的用法
    CentOS 6.4下编译安装MySQL 5.6.14 (转)
  • 原文地址:https://www.cnblogs.com/he-qing-qing/p/13592318.html
Copyright © 2011-2022 走看看