zoukankan      html  css  js  c++  java
  • Mysql基础操作语句

    SQL

    简单的增删改查

    不区分大小写, 表名和字段名可不加引号

    查询语句

    SELECT * FROM `table_name`;
    

    -- 注释 CTRL+/ : 注释 CTRL+/ :

    取消注释 /* */ : 多行注释

    创建表和字段

    CREATE TABLE `table_name`(
        `name` VARCHAR(20),
        `age` int,
        `sex` VARCHAR(10)
    );
    

    创建表和字段(添加ID字段,加主键)

    CREATE TABLE `table_name`(
        `id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
        `name` VARCHAR(20),
        `age` INT,
        `height` DECIMAL(5,2)
    );
    

    在表中插入行

    alter table 表名 add column 字段名 varchar(50) after/before 字段名;
    

    删除表

    1. DROP TABLE `table_name`;  -- 如果表不存在语句会报错
    2. DROP TABLE IF EXISTS `table_name`;  -- 表不存在也不会报错
    

    添加一行数据

    1. 所有字段都设置值  
    INSERT INTO `table_name` VALUES (值, 值, 值);
    
    2. 单独字段设置值
    INSERT INTO table_name(col_name) VALUES (值);  
    INSERT INTO table_name(col_name, col_name) VALUES (值, 值);
    
    3. 插入多行
    insert into table_name values (...), (...), (...)...
    

    修改数据

    update table_name set col_name=value, col_name=value, col_name=value where 条件;  
    

    删除数据

    1. delete from table_name where 条件; -- 不加条件删除所有数据, 保留表结构
    2. truncate table table_name;  -- 删除表中所有数据, 保留表结构
    
    1. truncate和delete删除所有数据时, 当id自增时, 继续添加数据, delete删除的数据从之前的自增继续自增, truncate删除的数据从1开始自增
    2. 速度上, drop > truncate > delete
    3. 逻辑删除: 设置标志位, 不实际删除数据, 只是标志该数据被删除

    数据操作

    查询所有字段

    selete * from 表名;
    

    查询部分字段

    select 字段1, 字段2, 字段3 from 表名;
    

    起别名

    1. 给表起别名  
    select 别名.字段1, 别名.字段2 from 表名 as 别名;  
    2. 给字段起别名  
    select 字段1 as 别名,  字段2 as 别名 from 表名;
    

    起别名时as可以省略, 但是不建议省略, 别名只在查询结果中起作用, 表本身不受影响

    去重

    select distinct 字段1, 字段2 ... from 表名;
    eg: 
    select count(distinct 字段) from 表名; -- 去重后统计数量
    

    结果中一行记录另一行记录完全一样, 才算是重复数据

    条件查询

    使用where子句对表中的数据进行筛选

    1. select 字段 from 表名 where 条件;
    

    条件运算符: =, <, >, >=, <=, !=, <>
    逻辑运算符: and, or, not

    模糊查询

    关键字: like , %代表多个任意字符, _代表一个任意字符

    select * from 表名 where 字段名 like 条件;
    

    范围查询

    关键字: in, between...and...

    1. select * from 表名 where 字段名 in(条件,条件,条件);  
    2. select * from 表名 where 字段名 between 条件1 and 条件2;
    3. select * from 表名 where 字段名 not in(条件,条件,条件);  
    4. select * from 表名 where 字段名 not between 条件1 and 条件2;
    

    between...and... -> 条件1要小于等于条件2

    空判断

    关键字: is , null是空,''是空字符串

    select * from 表名 where 字段名 is null;
    select * from 表名 where 字段名 is not null;
    

    排序

    1. select * from 表名 order by 字段 asc | desc, 字段 asc | desc;  
    2. select * from 表名 order by convert(字段 using gbk);  
    

    asc可以省略, 默认升序, 多个字段排序按顺序排序
    默认排序是排序数字和字母, 中国默认按照文字编码排序, 可使用convert(), 按开头字母排序

    聚合函数

    常用聚合函数: count(), max(), min(), sum(), avg()
    count函数内可以写*, 也可以写某一列, 如果有null, 不会统计在内
    聚合函数不能用在where子句后面

    分组

    目的: 对每一组数据进行统计

    select 字段,聚合函数() from students group by 字段;
    eg:  
    select class, max(age), min(age), avg(age) from students group by class;
    

    按照某个字段分组后, select后只能写聚合函数和分组那个字段的名称

    分组后的数据筛选

    select 字段,聚合函数() from students group by 字段 having 条件(聚合);
    eg:
    select sex, count(*) from students group by sex having count(*)>5;
    

    where和having对比

    • where是对from后面指定的表进行数据筛选, 属于对原始数据的筛选
    • having是对group by的结果进行筛选
    • having后面的条件中可以使用聚合函数, where不可以

    获取部分行(分页)

    关键字: limit , 当数据量过大时, 需要分页查询

    select * from 表名 limit start,count;
    

    start(索引) : 从0开始, 拿count条, start可省略, 默认0, count可超过实际数量

    select .... from 表名 .....;
    组合查询使用顺序:
    where  
    group by  
    order by  
    limit
    

    连接查询

    内连接

    select * from 表1 inner join 表2 on 表1.列 = 表2.列;
    扩展语法(不建议使用, 效率低):
    select * from 表1,表2 where 表1.列 = 表2.列;
    eg:
    select * from students stu inner join scores sc on stu.studentno = sc.studentno inner join courses cs on sc.courseno=cs.courseno;
    eg:
    select stu.name 姓名, cs.name 课程, score 成绩 from students stu
    inner join scores sc on stu.studentno = sc.studentno
    inner join courses cs on sc.courseno=cs.courseno
    where stu.name='诸葛亮' order by score desc;
    

    连接条件最重要的是on后面的条件, 内连接只显示匹配成功(交集)的结果

    左连接

    select * from 表1 left join 表2 on 表1.列 = 表2.列;
    

    左连接是以左表为主表, 里面所有数据全部显示, 右表没有对应表数据时以null补全

    右连接

    select * from 表1 right join 表2 on 表1.列 = 表2.列;
    

    右连接是以右表为主表, 里面所有数据全部显示, 左表没有对应表数据时以null补全

    自关联

    自己连接自己, 数据拥有层级关系时使用

    select * from 表 别名 inner join 表 别名 on 条件;
    eg:
    select * from areas a1
    inner join areas a2 on a1.aid=a2.pid
    where a1.atitle='河南省';
    

    子查询

    一个select语句里嵌套一个select语言, 主查询, 子查询

    • 子查询嵌入到主查询中
    • 子查询是辅助主查询的, 充当条件或者充当数据源
    • 子查询是可以独立存在的语句, 是一条完整的select语句

    标量子查询: 子查询结果只有一行一列
    列子查询: 子查询结果有一列多行
    行子查询: 子查询结果有一行多列
    表子查询: 充当数据源, 充当数据源时, 必须要起别名

    eg:
    select * from students where age=(select min(age) from students);
    
  • 相关阅读:
    PHP $_SERVER变量
    Buddy system伙伴分配器实现
    Linux iconv使用
    内存管理(memory allocation内存分配)
    内存碎片
    《STL源码剖析》chapter2空间配置器allocator
    Effective C++学习笔记:初始化列表中成员列出的顺序和它们在类中声明的顺序相同
    c++ explicit
    《STL源码剖析》环境配置
    C++ STL的各种实现版本
  • 原文地址:https://www.cnblogs.com/hellomrr/p/10680100.html
Copyright © 2011-2022 走看看