MySQL数据库基础_表&简单查询

  • 创建表

create table IF NOT EXISTS 表名(
    列名 列类型,
    ....
    列名 列类型
);
  • 查看当前数据库中所有表名

show tables; 
  • 查看指定表创建语句

show create table 表名;
  • 查看表结构

desc 表名;
  • 删除表

drop table 表名;

简单查询

  • 查询一个字段

    • select 字段名 from 表名;
  • 查询两个或者多个字段

    • select 字段名1,字段名2 from 表名;
  • 查询所有字段

    • 第一种是把所有字段名都写上

      select a,b,c,d... from 表名
    • 第二种:可以使用*

      select * from 表名

      注意: 使用*效率低,查询不建议使用

  • 给查询起别名

    select 字段名 as 别名 from 表名

    as关键字可以省略,(别名里面不能有空格,会报错) 可以使用单引号解决

    字段是可以参加+-*/运算的

条件查询

  • 语法格式

    select
        字段1,字段2,字段3...
    from
        表名
    where
        条件
    • = 等于

    • <>或!= 不等于

    • <小于 、 大于>

    • <=小于等于 、 >= 大于等于

    • between.....and..... 两值之间

    • is null 为null (is not null 不为空)

    • and 并且

    • or 或者

    • in 包含,相当于多个or ,( not in 不在这个范围中)

      select 
          字段名1,字段名2,字段名3 
      from 
          表名
      where
          id in(3,6,8,9);
  • 模糊查询

    • like ,支持%和下划线匹配

      %匹配任意多个字符

      下划线: 任意一个字符

      找出名字里含有中的

      select name from 表名 where name like '%中%';

      找出第三个字母是s的

      select name from 表名 where name like '__s';

      找出名字里有"_"的 ,加\转义

    • select name from 表名 where name like '%\_%'

       

排序

  • 查询所有员工薪资,排序 (默认升序)

    select
        name,salary
    from
        表名
    order by
        salary;
  • 指定降序、asc是指定升序

    select
        name,salary
    from
        表名
    order by
        salary desc;
  • 两个或多个字段排序

    查询名字和薪资,薪资升序 ,薪资一样按照名字升序排列

    select
        name,salary
    from
        表名
    order by
        salary asc,name asc;

    排序总是在最后执行输出

posted @ 2021-07-29 21:36  大萝卜萌萌哒  阅读(51)  评论(0编辑  收藏  举报