MySQL(三)

时间:2019-06-12
本文章向大家介绍MySQL(三),主要包括MySQL(三)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
一,MySQL查询 DQL
  1,准备相关库和表
    create datebase
    create table
  2,简单的查询
      select 查询 from 从 where 哪里,条件
    1》查名称价格
      select 字段,字段1,……from表名;
    2》查商品所有列信息
      select * from 表名;
    3》过滤重复数据
      select distinct 字段,字段1,……from 表名;
      select distinct price from product;
    4》给表起别名
      select * from product [as] p; select * from product p;
    5》给表中字段起别名
      select pname as 商品名称,price as 商品价格 from product;
    6》查询语句中可以直接进行数据运算
        select(1+1);
      查询商品名和价格,涨价1000
        select pname,price+1000 from product;
  3,条件查询
    运算符:
      1,比较
        基本比较:> < >= <= = <> !=
        查询区间:between ……and >=&&<=
        多个条件:in(10,20) in(……)
        表示空的:is null
        表示非空:is not null
        模糊查询:使用关键字 like
        多个任意字符 %
        单个任意字符 _
      2,逻辑
        与:and &&
        或:or ||
        非:not !
    1》查询商品名为“海澜之家”的所有商品信息
      select * from product where pname='海澜之家';
    2》模糊查询
      select * from product where pname=‘海澜%’;
      select * from product where category_id is null;
      select * from product where category_id is not null;
      select * from Product where not (category_id is null);
  4,排序查询
      对查询后的结果进行排序 默认升序 ASC 降序 DESC
    1》升序价格
      select * from 表名 order by 字段 顺序;
      select * from product order by price;
      select * from product order by price ASC;
      select * from product order by price DESC;
    2》去掉重复价格,降序
      select distinct price from product order by price DESC;
    3》分类的降序
      select *|字段 from 表名 [where 条件] order by 要排序字段 ASC|DESC;
  5,聚合查询
    对某列数据进行查询,返回结果是一个单一的值,会忽略null值
    ★,count 统计指定列不为null的值的个数,也就是行数
      sum 统计指定列的数值和,如果指定列不是数值类型,结果为0
      avg 统计指定列的平均值,如果指定列不是数值类型,结果为0
      max 统计指定列的最大值,如果指定列是字符串类型,使用字符串运算规则排序
      min 统计指定列的最小值,如果指定列是字符串类型,使用字符串运算规则排序
    格式:
      select count(字段|*),sun(字段),max(字段),min(字段),avg(字段 from 表名;
  6,分组查询
    格式:
      select 被分组字段,sum,avg,count,max,min(*|字段)from 表名 [where 条件] group by 被分组字段[having 条件];
      select category_id,count()from product group by category_id;
    ★,where 用于分组前进行条件过滤
      having 用于分组后对分组完的数据进行过滤
 
二,备份恢复
  DOS命令:
    备份:mysqldump -uroot -proot day02>d:\day02.sql
    恢复:mysql -uroot -proot day02<d:\day02.sql

原文地址:https://www.cnblogs.com/kide1412/p/11009854.html