mysql 数据库的常用操作

发布于 更新于
本文目录
  1. mysql数据库的常用操作
  2. 增
  3. 删
  4. 改
  5. 查
  6. 其他
  7. sql执行顺序

技术文章Linux与数据库

本文主要介绍mysql数据库的查询操作,捎带脚增删改操作

mysql数据库的常用操作

本文主要介绍mysql数据库的查询操作,捎带脚增删改操作

增

  • insert into

    • insert into table (字段) values (数据)(数据)...

    • insert into table values (数据)(数据)...

    • insert into table (列名,列名...png) select (列名,列名...png) from 表

删

  • drop

    • drop datebase "数据库名" -- 删除数据库

    • drop table “表名” -- 删除表

  • delete from

    • delete from table -- 删除表中数据

    • delete from table where 条件 -- 按条件删除表中数据

改

  • update

    • update “表名” set 字段=value where 条件

查

  • select

    • 单表查询

      select * from table where 条件
      
      select id as **c_id** from table where 条件
    • 多表关联查询

      - **内联查询**
      
          ![image.png](/images/upload/file/2019/c4c0de63-99be-497e-a9d5-53bfa92b0bf8.png)
      
          select * from table1,table2... where 关联条件 and 查询条件
      
          select * from table1 inner join table2 on 关联条件 ... where 查询条件
      
       - **左联查询**
      
          ![image.png](/images/upload/file/2019/fa7d5a31-f631-43c0-986c-d92d547f6cb2.png)
      
          select * from table1 left join table2 on 关联条件 ... where 查询条件
      
        - **右联查询**
      
            效果与左联查询效果相反
      
            select * from table1 right join table2 on 关联条件 ... where 查询条件
      
         - **union查询**
      
            左联查询、右联查询的查询结果去重(去掉重复数据)后进行合并
      
            select * from table1 left join table2 on 关联条件
      
            union (union all 为不去重将结果全部合并)
      
            select * from table1 right join table2 on 关联条件

    • 高级查询

      • order by -- 对查询结果进行排序

        select * from table where 条件 order by 字段(需要排序的字段) asc\desc

        select * from table order by 列1 asc,列2 desc... (根据多列进行排序,若列1数据相同,根据列2进行排序)

      • in -- 条件在数据集\子查询中

        select * from table where 条件字段 in 数据集\子查询

      • like -- 根据条件模糊查询

        select * from table where 条件字段 like '%_值' (%--任意字符、_--每个下划线代表一个字符)

      • group by -- 根据某字段进行分组查询

        select * from table where 条件 group by having

      • between and -- 查询条件在某个区间之间

        select * from table where 条件字段 between ... and ...

      • distinct -- 对查询结果进行去重

        select distinct(去重字段) from table where 条件

      • limit -- 分页

        select * from table where 条件 limit m,n

        select * from table where 条件 limit n offset m (从偏移m个数据,查询n条)

  • 数据库查询常用函数

    • 数值相关函数

      • max() -- 最大值

      • min() -- 最小值

      • avg() -- 平均值

      • sum() -- 求和

      • count() -- 计数

      • median() -- 中位数

    • 日期相关函数

      • sysdate() -- 当前日期时间

      • curdate() -- 当前日期

      • curtime() -- 当前时间

      • year() -- 获取日期的年份

      • month() -- 获取日期的月份

      • date_add(日期,interval 变更值 单位) -- 变更日期

    • 字符串相关函数

      • concat() -- 拼接字符串

      • length() -- 获取字符串长度

      • substr() -- 截取字符串

其他

  • desc “table”

列出表的信息(包含表结构、字段、字段类型、主键、外键等)

  • use "datebase"

打开数据库

  • <>

条件中的不等于

sql执行顺序

  • select ... from ... where ... group by ... having ... order by ...

当一条sql语句同时存在where、group by、having、order by关键字时,执行顺序:

顺序

关键字

说明

1

from

从哪个表进行数据查询

2

where

执行where xx对全表数据做筛选,返回第1个结果集

3

group by

针对第1个结果集使用group by分组,返回第2个结果集

4

having

针对第2个结集执行having xx进行筛选,返回第3个结果集

5

select

针对第3个结果集中的每1组数据执行select xx,有几组就执行几次,返回第4个结果集

6

order by

针对第4个结果集排序

(1) FROM left_table

(2) ON join_condition

(3) join_type JOIN right_table

(4) WHERE where_condition

(5) GROUP BY group_by_list

(6) HAVING having_condition

(7) SELECT

(8) DISTINCT select_list

(9) ORDER BY order_by_condition

(10) LIMIT limit_number

1.sql执行顺序参考链接

2.sql多表关联查询参考链接

来源:testingpai

继续阅读