zoukankan      html  css  js  c++  java
  • 【个人笔记】《知了堂》前端mySql基础

    指定列之后添加:

        ALTER TABLE 表名 ADD 添加的新列名 INT AFTER 指定列之后

    第一个位置:

        ALTER TABLE 表名 ADD 添加的新列名 varchar(20) AFTER FIRST

    更新表中的数据

        update 语句可用来修改表中的数据, 基本的使用形式为:

        update 表名称 set 列名称=新值 where 更新条件;

        使用示例:

          将id为5的手机号改为默认的"-": update students set tel=default where id=5;

          将所有人的年龄增加1: update students set age=age+1;

          将手机号为 13288097888 的姓名改为 "张伟鹏", 年龄改为 19: update students set name="张伟鹏", age=19 where tel="13288097888";

    删除表中的数据

        delete 语句用于删除表中的数据, 基本用法为:

        delete from 表名称 where 删除条件;

        使用示例:

          删除id为2的行: delete from students where id=2;

          删除所有年龄小于21岁的数据: delete from students where age<20;

          删除表中的所有数据: delete from students;

    创建后表的修改

        alter table 语句用于创建后对表的修改, 基础用法如下:

        添加列

          基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];

         示例:

          在表的最后追加列 address: alter table students add address char(60);

          在名为 age 的列后插入列 birthday: alter table students add birthday date after age;

        修改列

          基本形式: alter table 表名 change 列名称 列新名称 新数据类型;

          示例:

          将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default "-";

          将 name 列的数据类型改为 char(16): alter table students change name name char(16) not null;

        删除列

          基本形式: alter table 表名 drop 列名称;

          示例:

          删除 birthday 列: alter table students drop birthday;

        重命名表

          基本形式: alter table 表名 rename 新表名;

        示例:

          重命名 students 表为 workmates: alter table students rename workmates;

         删除整张表

          基本形式: drop table 表名;

        示例: 删除 workmates 表: drop table workmates;

         删除整个数据库

          基本形式: drop database 数据库名;

        示例: 删除 samp_db 数据库: drop database samp_db;

    查询所有:

    • select * from 表

    指定列:

    • select 列名 from 表名

    as别名:

    • select *,列名 as '中文名字' from 表名 where 列='查询谁'

    distinct筛选重复的数据:

    • select distinct 列名 from 表名

    添加列:

    • alter table 表名 add 添加的列名 int after 谁的后面

    between and 包含:

    • select * from 表名 where 列名 between 条件 and 条件

    in简化or:

    • select * from 表名 where 列名 in(查询值1,值2,值3...)

    不包含 not in:

    • select * from 表名 where 列名 not in(不包含值1,值2,值3...)

    模糊查询:
    查询所有:

    • select * from 表名 where 列名 like '查询什么'

    一个下划线代表一个占位符:

    • select * from 表名 where 列名 like '张__'

    is NUll 空 空值没有输入数据:

    • select * from 表名 where 列名 is not null

    不为空 is not null:

    • select * from 表名 where 列名 is not null

    查询结果排序order by:

    • select * from 表名 order by 列名 默认升序
    • select * from 表名 order by 列名 desc 降序
    • select * from 表名 order by 列名 asc 升序
    • select * from 表名 order by 列名 asc列名,desc列名。注:先升序,在降序。

    注:一般用于成绩查询及商品价格等;

    返回限定行:

    • --limit 数字
    • --limit 数字n,数字m 代表从多少行开始,m数多行
    • select * from 表名 limit 2; --限定行
    • select * from 表名 limit 2,4; --开始行,结束行

    注:做分页的时候比较常用;

    聚合函数:

    计数count(列):

    • select count(列(也可以使用*号)) as 别名 from 表名 where 列名= 值

     :可以使用  as 别名;

    求和:

    • select sum(列(也可以使用*号)) from 表名


    平均:

    • select sum(列)/count(*) from 表名
    • select avg(列) from 表名 --默认不包含空
    • selec avg(ifnull(列,0)) -- 如果是空数据 补成0 在求平均

    最大,最小:

    • select max(列),min(列) from 表名

    分组:

    • select count(*) from 表名 where 列=值
    • select count(*) from 表名 where 列=值

      :首先要知道总数,比如一个班:第一列显示那个班,第二列显示多少人;

    • select 列,count(*) from 表名 group by 列名

    具体统计:

    • select 列,count(*) from 表名 where 列=值 group by 列名 --只统计一类
    • select 列,count(*) from 表名 group by 列名,列名 --统计两个不同类;

     多条件查询:

    • -- 查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
    •  select CNO,SUM(DEGREE),count(*) from SCORE where CNO LIKE '3%' group by CNO HAVING(count(*)>5);

    注:SELECT 课程列,SUM(成绩列),COUNT(*) FROM 表名 WHERE 课程列 LIKE '3%' GROUP BY 课程列分组 HAVING(COUNT(*)>5);

     相关子查询:

      --select嵌套

        select 列名,(select 列名 from 表名 where 子列名=主列名) from 主表名

    非相关子查询:

      --from嵌套

        select * from (select * from 子表名 where 子列名=值) as 子t1  where  子t1.主列名=值;

      --where嵌套

        select * from 主表名 where 主列名=(select 子列名 from 表名 where 子列名=值)

      示例:

        --查询与老张一个班的学生有哪些

          select * from t_student where s_classid=(select s_classid  from t_student where  s_name='老张')

  • 相关阅读:
    清理git提交记录并不能达到真正硬盘“瘦身”
    virtualbox虚拟机异常暂停
    百兆带宽升千兆-番外篇
    流水文之百兆带宽升千兆
    OSS设置静态网站托管+CDN加速OSS域名
    每日一坑-exsi中win虚拟机调分辨率
    nginx筛选字段+excel统计
    树莓派使用Samba共享文件
    chrome无法使用独显解决
    解决jdk16安装后无jre目录的问题
  • 原文地址:https://www.cnblogs.com/1542986913Yu/p/7550607.html
Copyright © 2011-2022 走看看