mysql基本操作(重点)

  1. 显示数据库

    show  databases 
  2. 进入指定数据库

    use 数据库名称
  3. 创建数据库
    create database 数据库名称 default character set=utf8
  4. 删除数据库
    drop database 数据库名称

二、数据库表的操作

  1. 创建表:
    create tabale studentinfo(
    name varchar(10) not null,
    sex char(10) null,
    age int(5),
    phone bigint(11)
    )
    
    studentinfo 为所创建的表名
  2. 删除表
    drop table 表名;
  3. 新增表数据
    # 一次增加一条数据
    
    insert into studentinfo (name,sex,age)
    values('黎诗','','12')
    #  一次增加多条数据
    
    insert into studentinfo (name,sex,age) values('黎诗','','21'),('诗诗','','22')
    
    insert into 表名称 (字段名称,多个以,间隔) values (具体的值多个以,间隔)
  4. 修改
    update studentinfo set name = '黎诗' where name = '小诗诗'
  5. 删除
    delete from 表名 where 条件
    #拷贝表结构+记录
     create table day43.user select host,user,password from mysql.user;
    #只拷贝表结构
     create table day43.user select host,user,password from mysql.user where 1=2;
    拷贝

三、数据库表的简单查询(一个简单小例子)

  1. 查询所有人员
    select * from people
  2. 只查询人员的姓名和年龄
    select p_name,p_age from people
  3. 查询年龄为20岁的人有哪些?
    select * from people where p_age = '20'
  4. 查询60岁一下的人员有哪些?
    select * from people where p_age < '60'
    
    查询年龄不等于60岁 的人员
    select * from people where p_age <> '60' (推荐,正式)
    
    select * from people  where p_age != '60' (非常规)
    
    常见的逻辑运算符 <, >, =, <=, >=, <>, !=
  5. 查询50岁以上并且工资大于8000的人员有哪些?(逻辑运算符查询)
    select * from people where p_age > 50 && p_sal < 8000
    
    #  注意: and (&&)用于连接两个条件 表示并且意思
    
    #           or(||) 用于连接两个条件表示 或者意思

    # 逻辑运算符: = , <, >, !=, <> , <=, >=
  6. 查询姓张的人员(模糊查询)
    select * from people where p_name LIKE '张%',

    select * from prople wherer p_name LIKE '%张%' # 中间有张字的

    # select * from 表名 where 字段 like '%张'
    # like:表示模糊查询
    # 以什么开头 : 's%'
    # 以什么结尾: '%s'
    # 包含: '%s%'
  7. 查询哪些人属于武当 华山 嵩山(集合查询)
    select * from people where p_menpai = '武当' or p_menpai = '华山' or p_menpai = '嵩山';
    
    select * from people where p_menpai in ('武当','华山','嵩山');
    
    # select * from 表名 where 字段 in('值1','值2','值3')
    # in : 表示 集合
    # not in:表示 反向集合
  8. 查询工资在5000-8900的人员有哪些?(区间查询)
    select * from people where p_sal >= 5000 and p_sal <= 8900;
    
    select * from people where p_sal between 5000 and 8900;

    # select * from 表名 wheere 字段 between 值1 and 值2
    # 注意 : between...and... 表示区间查询
  9. 查询所有人员,要求工资倒序排序(排序)
    select * from people where p_sal > 3000 ORDER BY p_sal desc

    # asc 为正序 (默认)
    # desc 为倒序
  10. 查询年龄为21岁人员的领导者(嵌套查询)
    # select p_learder from people where p_age = '21'
    
    # selecr * from people where p_id = 'p003'
    
    select * from people where p_id = (selest p_learder from peopple where p_age = '21')
     
    # select * from 表 where 字段 in (select 字段 from 表 where id = '值1'
    # () 优先执行
    # 遇到 '=' 值唯一,遇到 in 值为集合
  11. 查询当前人员中谁的工资最高?(嵌套函数)
    # select max(p_sal) as p_sal from people 

    select p_name from people where p_sal = (select max(p_sal) as p_sal from people )

    # max () 表示最大值
      as 表示 起别名
  12. 查询当前人员中谁的工资最低?
    select p_name from people where p_sal = (select min(p_sal) from people)
    SELECT p_name,p_sal from people where p_sal = (SELECT MIN(p_sal) as p_sal FROM people)
    

    # 注意: min 为最小值
  13. 查询所有人员的平均工资是多少
    select avg(p_sal) from people
    
    # 注意:avg()表示平均值
  14. 查询所有人员的工资总和是多少
    select sum(p_sal) from ren
    
    # 注意 sum()求和
  15. 查询目前有多少个人员?
    select count (p_id) from people
    
    # 注意  count(主键)  表示查询表中数据的总条数
  16. 查询各门派的平均工资是多少
    select avg(p_sal),p_menpai,p_name from people GROUP BY p_menpai order by avg(p_sal) desc
    
    # 注意: group by  表示分组
  17. 查询武当派最高工资是谁
    select p_name from people where p_sal = (select  max(p_sal)from people where p_menpai = '武当'and p_menpai ='武当'
  18. 查询当前武林中有哪些门派
    select p_menpai from people GROUP BY p_menpai
    
    
    select  distinct p_menpai,p_name from people
    
    #  注意 : distinct 表示去重复查询,要求查询的所有字段必须一样,才认为是重复数据
  19. 查询当前武林中有哪些门派和门派的平均工资是多少
    select p_menpai ,avg(p_sal) from people group by p_menpai
  20. 查询当前人员表的中的第3条数据到第七条数据(分页)
    select * from people limit 2,5
    
    #  注意     limit 表示分页
    # 参数1 : 表示从第几条开始查询,下标从0开始
    # 参数2 : 表示每次查询多少条数据
  21. 查询没有门派的人员有哪些?(条件时 用is null    修改时 用= null)
    select * from people where p_menpai is null
    
    # 表示查询字段为null 的数据  为 is
    
    select * from people where p_menpai = ''
    
    # 表示查询字段为 ''的数据
    
    
    # 别的例子
    updata people set p_menpai = null where p_id = 'p_008'
    
    # 注意  修改字段为null 时,要写 =
  22. 查询武当派下有哪些小弟
    select * from people where p_leader = (select p_id from people where p_menpai = '武当' and p_leader = '0')
    
    select * from people where p_mempai = '武当' and p_leader != '0'
  23. 查询各门派的工资总和按倒序/正序排列
    select sum(p_sal) sal,p_menpai from people group by p_menpai order by sal
  24. 查询人员并显示门派所在位置
    select * from people,location where people.p_menpai = location.a_name

    # 注意: 如果多表联合查询不加条件则会出现(笛卡尔乘积)
    #   : 在使用多表联合查询时,一定要加条件
    # 结果: 符合两个表条件的结果
  25. 查询人员表,如果人员门派存在位置则显示位置信息,不存在则不显示位置
    select * from people left join location on people.p_menpai = location.a_name
    
    # 左连接查询
    #     注意:on  表示条件 专门配置 left  join 来使用
    #    特点:左表数据全要,右表的数据与左表数据相匹配则显示,不匹配则以NULL填充
            
  26. 查询位置表,如果人员的门派有位置信息则显示人员,没有则不显示
    select * from people right join location on people.p_menpai = location.a_name
  27. 查询登记了地理位置的门派人员信息
    select * from people inner join location on people.p_menpai = location.a_name
原文地址:https://www.cnblogs.com/jassin-du/p/8001588.html