数据库常用SQL语句

  • 显示所有的数据库
    • show databases;
  • 新建数据库
    • create database if not exists 数据库名  default character set = 'utf8';
  • 删除数据库
    • drop database (if exists) 数据库名;
  • 使用表
    • use 数据库名;
  • 新建表
    • create table if not exists 表名(

        id int unsigned primary key auto_increment,

        name varchar(10),

        gender char(1),

        birthday date,

        time datatime,

        score decimal(4,1)

      );

  • 删除表
    • drop table (if exists) 表名;
  • 增加数据
    • insert into 表名 (字段1, 字段2, 字段3) values

      (字段1值, 字段2值, 字段3值),

      (字段1值, 字段2值, 字段3值);

  • 修改数据
    • update 表名 set 字段1 = ‘字段值1’, 字段2 = ''字段值2 where 条件;
  • 删除数据
    • delete from 表名 where 条件;                   # 删除表格里面已有的数据
    • truncate table 表名;                                  # 相当于删除表后再建一个同名的空表
  • 逻辑删除(标记)
    • 添加一个字段isdelete,删除-1,未删除2
    • update 表名 set isdelete = 1 where id = 1;                # 将id为1的数据的isdelete字段值改为1,即标记为已删除
    • select * from 表名 where isdelete = 0;                      # 查询出未被标记删除的所有数据
  • 查询
    • select * from 表名;                                                                                                           # 简单查询
    • select 字段1 (as) 别名1, 字段2 (as) 别名2 from 表名 where 条件;                                  # 别名
    • select distinct 字段1, 字段2 from 表名 where 条件                                                         # 去重
  • where 条件
    • >  <  =  !=(<>)  <=  >=                                                                                                      # 比较运算

    • and or not                                                                                                                        # 逻辑运算
    • like '孙%'                                                                                                                          # 以孙开头的任意长度字符(模糊查询)
    • like '孙_'                                                                                                                           # 以孙开头的两个字符(模糊查询)
    • in (值1, 值2, 值3)                                                                                                             # 范围查询,功能同or
    • between 10 and 80                                                                                                         # 范围查询,功能同and
    • is (not) null                                                                                                                      # (不)为空,null表示未填写
  • 排序
    • order by 字段1 desc/asc, 字段2 desc/asc                                                                      # desc (降序)、asc(升序,默认)
    • order by convert(字段名 using gbk)                                                                               # 将某个字段按照中文排序
  • 聚合函数
    • select count(*) from 表名;                                                                                              # 计数, ps:为null的数据不会被统计进去
    • select max(字段名) from 表名;                                                                                      # 最大值
    • select min(字段名) from 表名;                                                                                       # 最小值
    • select avg(字段名) from 表名;                                                                                       # 平均值
    • select sum(字段名) from 表名;                                                                                      # 求和
  • 分组
    • group by 字段名                                                                                                            # 按照某字段进行分组,有去重的作用,一般与聚合函数搭配使用
    • 所有select的字段,除聚合函数中的字段,都必须在group by中出现。只要满足这个规则就可以 
  • 分组后过滤
    • select count(*) from 表名 where sex = '男';                                                                  # 查询所有男生的人数(方法1---先过滤,在聚合)
    • select count(*) from 表名 group by sex having sex = '男';                                            # 查询所有男生的人数(方法2---先分组聚合再过滤)
  • 分页
    • select * from 表名 limit 2;                                                                                          # 查询前2条数据,相当于limit  0, 7
    • select * from 表名 limit 2, 3;                                                                                        # 跳过前2条,查询其后的3条
    • select * from 表名 limit (页数-1) * 每页显示的条数, 每页显示的条数;                         # 分页功能
  • 连接查询(多表查询/等值连接)
    • select * from 表1, 表2;                                                                                                # 多表查询,但是会造成 “笛卡尔积”
    • select * from 表1, 表2 where 表1.字段名 = 表2.字段名;                                             # 等值连接进行多表查询,先连接另一张表的数据(必然会造成笛卡尔积,产生表1数据 * 表2数据的临时表),再进行where 的过滤判断
  • 连接查询(内连接)
    • select * from 表1 inner join 表2 on 表1.字段名 = 表2.字段名;                                    # 连接的过程中先判断on条件,再连接另一张表的数据(不会产生临时表,不会生成笛卡尔积,性能高)
  • 连接查询(左连接)
    • select * from 表1 left join 表2 on 表1.字段名 = 表2.字段名;                                       # 以左边表(表1)的数据为准,即显示全部的表1数据
  • 连接查询(右连接)
    • select * from 表1 right join 表2 on 表1.字段名 = 表2.字段名;                                     # 以右边表(表2)的数据为准,即显示全部的表2数据
  • 自关联
    • select * from 表1 s1, 表1 s2 where s1.字段名 = s2.字段名;                                       # 一张表需要用到多次
  • 子查询
    • 标量子查询(子查询返回的结果是一行一列(一个值))
      • select * from student where age >
        (select avg(age) from student);                    # 查询出所有年龄大于平均年龄的学生信息

    • 列子查询(子查询返回的结果是多行一列(多个值))
      • select score from 成绩表 where sid in (select sid from 学生表 where age = 18);    # 查询出所有年龄为18岁的学生的成绩

    • 行子查询(子查询返回的结果是一行多列)
      • select * from student where (sex, age) = (select sex, age from student where sex = '男' order by age desc limit 1);     # 查询出男生中年龄最大的学生的信息

    • 表子查询(子查询返回的结果是多行多列(临时表))
      • select cname, score from 成绩表
        inner join
        (select * from 课程表 where cname = ('数据库', '系统测试')) as c on 成绩表.课程编号 = c.课程编号;                     # 查询数据库和系统测试课程的成绩

    • 关键字
      • in ----------------- 范围
        • select score from 成绩表 where sid in (select sid from 学生表 where age = 18);    # 查询出所有年龄为18岁的学生的成绩
      • (> < =)any/some  () ------------------ =any/some 相当于 in ,>any/some 大于最小的,<any/some 小于最大的
      • (> < !=)all ()              ----------------- !=all 相当于not in ,>all 大于最大的,<all小于最小的
原文地址:https://www.cnblogs.com/SakuraYuanYuan/p/11070572.html