• MySQL常用命令及其案例


    一、Windows命令行 CLI

    1、启动数据库服务:net start mysql
    2、登录数据库:mysql -uroot -p123456
    3、退出数据库:net stop mysql
    4、关闭数据库服务:net stop mysql

    二、数据库操作命令

    命令 作用
    show databases 显示所有数据库列表
    create database 数据库名 创建数据库
    create database 数据库名 character set utf8 创建数据库并设置编码utf8
    alter database 数据库名 character set utf8 修改数据库编码
    use 数据库名 选中指定名称数据库
    show tables 显示数据库中所有表
    desc 表名 查看表结构
    drop database 数据库名 删除指定数据库

    三、数据库的导入与导出

    四、数据库语言

    DML:是指对数据库中的表记录的操作,主要包括UPDATE、INSERT、DELETE,这3条命令是用来对数据库里的数据进行增删改操作。
    DDL:主要的命令有CREATE、ALTER、DROP等,主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,他们大多在建立表时使用。

    五、DML

    (一)、查询模型
    语法:
    SELECT
    [字段1 [as 别名1],[函数(字段2) ,]…字段n]
    FROM 表名
    [WHERE where条件]
    [GROUP BY 字段]
    [HAVING where_contition]
    [order 条件]
    [limit 条件]
    WHERE条件查询
    数据库中常用的是where关键字,用于在初始表中筛选查询。它是一个约束声明,用于约束数据,在返回结果集之前起作用。
    基本语法: select 字段 from 表 where where条件;

    练习:

    1.主键为32的商品:
    select * from goods where goods_id=32;
    2.不属于第3个栏目的所有商品
    select * from goods where cat_id!=3;
    3.本店价格高于3000的商品
    select * from goods where shop_price>3000;
    4.本店价格低于或等于100的商品
    select goods_id,goods_name,shop_price, from goods where shop_price<=100;
    5.取出第4个栏目或第11个栏目
    select goods_id,goods_name,cat_id from goods where cat_id=3 or cat_id=11;
    6.取出100<=价格<=500的商品 shop_price>=100 and shop_price<=500
    select goods_name,cat_id,shop_price from goods where shop_price>=100 and shop_price<=500;
    between 100 and 500
    select goods_name,cat_id,shop_price from goods where shop_price between 100 and 500;
    7.取出不属于第3个栏目,且不属于第11个栏目的商品(and 或 not in 分别实现)

    select goods_name,cat_id from goods where catid!=3 and cat_id!=11;
    select goods_name,cat_id from goods where cat_id not in (3,11);
    8.取出第3个栏目下价格<1000或>3000并且点击量>5的商品
    select goods_name,cat_id,goods_id,shop_price,click_count from goods where cat_id=3 and (shop_price<1000 or shop_price>3000) and click_count>5;
    9.取出价格大于100且小于300,或大于4000且小于5000的商品
    select goods_name,cat_id,goods_id,shop_price from goods (shop_price>100 and shop_price<300) or (shop_price>4000 and shop_price<5000);
    10.取出名字以‘诺基亚’开头的商品
    select goods_id,goods_name from goods where goods_name like “诺基亚%”;
    11.取出名字为“诺基亚Nxx”的手机
    select goods_id,goods_name from goods where goods_name like “诺基亚N__”;
    12.取出名字不以‘诺基亚’开头的商品
    select goods_name from goods where goods_name not like “诺基亚%”;
    13.取出第3个栏目下面价格在1000到3000之间,并且点击量>5"诺基亚"开头商品
    select goods_name,shop_price,click_count from goods where cat_id=3 and click_count>5 and goods_name like “诺基亚%”;

    GROUPBY分组查询与统计函数
    统计函数
    如果我们想知道总用户数怎么办?
    查询谁是数据表里的首富怎么办?
    如果我们想知道用户的平均金额怎么办?
    如果我们想知道所有用户的总金额怎么办?
    函数名 作用
    count() 计算行数
    avg() 求平均值
    sum() 求和
    min() 求最小值
    max() 求最大值
    基本语法:select 函数(字段) from 表

    示例:

    1.查询所有商品的平均价格
    select avg(shop_price) from goods;
    2.查询最贵的商品
    select max(shop_price) from goods;
    3.查询最便宜的商品
    select min(shop_price) from goods;
    4.查询一共有多少种商品
    select count() from goods;
    5.查询商品的总价格
    select sum(shop_price) as allsum from goods;
    6.查询本店积压的商品总货款
    select sum(shop_price
    goods_number) as all from goods;

    GROUPBY分组查询

    GROUP BY 语句根据一个或多个列对SELECT结果集进行分组。
    基本语法:select * from 表 group by 字段

    案例:

    1.统计每个栏目下商品的平均价格
    select avg(shop_price) as avg,cat_id from goods group by cat_id;
    2.查询每个栏目下有多少种商品
    select count(*),cat_id from goods groupby cat_id;
    3.查询每个栏目下最贵的商品
    select max(shop_price),cat_id from goods group by cat_id;

    HAVAING 筛选

    用于对where和group by查询出来的结果集进行过滤,查出满足条件的结果集。它是一个过滤声明,是在查询返回结果集以后对查询结果进行的过滤操作。

    基本语法:select * from 表 group by 字段 having 条件
    示例:
    1.查询每个商品所积压的货款(库存* 单价)
    select goods_id,goods_name,goods_numbershop_price from goods;
    2.查询该店每个栏目下积压的货款
    select sum(goods_number
    shop_price),cat_id from goods group by cat_id;
    3.查询比市场价格省钱200以上的商品及该商品所省的钱(where having)
    select goods_id,goods_name,market_price-shop_price from goods whre market_price-shop_price>200;
    select goods_id,goods_name,market_price-shop_price as sheng from goods having sheng>200;

    如果我们把where中的market_price-shop_price赋值给sheng where sheng> 200这样会报错 unknown column ‘sheng’
    select goods_id,goods_name,market_price-shop_price as sheng from goods where sheng>200; //报错sheng列找不到
    where条件筛选的是磁盘上的表的字段,而sheng是结果集上的字段,结果集查出来的是临时放在内存里的,我们需要筛选结果集上的字段,我们可以将结果集当成表,进一步筛选,只不过不能用where需要用having, where 发挥的比较早,是针对磁盘上的表发挥作用,筛选出结果集。从结果集中进一步筛选需要用having,where与having同时存在,where要放到having前面。

    orderby排序

    排序是按照某个属性来排的,在数据库中是按照某个列来排的,那有正序也有倒序。
    基本语法:select 字段 from 表 order by 字段1 desc/asc,字段n desc/asc
    asc 升序排列,从小到大(默认)
    desc 降序排列,从大到小
    示例:
    1.按照价格进行排序
    select * from goods order by shop_price asc;
    select * from goods order by shop_price desc;
    2.按照栏目进行排序
    select * from goods order by cat_id asc;
    select * from goods order by cat_id desc;
    3.按照栏目升序排列后,在每个栏目中按照价格降序排列
    select goods_name,cat_id,shop_price from goods order by cat_id,shop_price desc;

    limit 限制取出条数

    order by一般和limit配合使用功能才会更强大
    基本语法:
    语法1 select 字段 from 表 order by 字段 关键词 limit 数量
    语法2 select 字段 from 表 order by 字段 关键词 limit offset,n
    limit n //限制取出条目
    limit offset,n //跳过offset条取出n条
    示例:
    limit 3 //取出0~ 3 取出前3
    limit 2,3;//取出前3到前5
    1.取出价格最高的前三名商品
    select goods_name,shop_price from goods order by shop_price desc limit 0,3;
    2.取出点击量前3到前5名的商品
    select goods_name,goods_id,click_count from goods order by click_count desc limit 2,3;

    多表联合查询:

    连接类型:

    类型 子类型 描述
    内连接 取得两个表中存在连接匹配关系的记录。内连接使用比较运算符根据每个表共有的列的值匹配两个表中的行。
    例如,检索 students 和 courses 表中学生学号相同的所有行。
    隐式的内连接 没有INNER JOIN,形成的中间表为两个表的笛卡尔积。
    显式的内连接 有INNER JOIN,形成的中间表为两个表经过ON条件过滤后的笛卡尔积。
    类型 子类型 描述
    外连接 外连接可以是左向外连接、右向外连接
    左外连接 左表中的所有数据全部取出,然后到对应的右表去找数据,如果找不到数据补null
    右外连接 右外连接是左外联接的反向联接,将返回右表的所有行,如果右表的某行在左表中没有匹配行,则将为左表返回空值。
    内连接
    隐式语法1 select 表1.字段 [as 别名],表n.字段 from 表1 [别名],表n where 条件;
    显式语法2 select 表1.字段 [as 别名],表n.字段 from 表1 INNER JOIN 表n on 条件;
    案例:
    取出boy和girl表数据,id相同者为一对
    //隐式内连接
    select boy.hid,boy.bname,girl.hid,girl.gname from boy,girl where boy.hid=girl.hid
    //显式内连接
    select boy.hid,boy.bname,girl.hid,girl.gname from boy inner join girl on boy.hid=girl.hid
    外联接
    左向外联接
    语法:select 表1.字段 [as 别名],表n.字段 from 表1 LEFT JOIN 表n on 条件;
    案例:
    所有男士站出来,找到自己的另一半,没有的以Null补齐

    select boy.hid,boy.bname,girl.hid,girl.gname from boy left join girl on boy.hid=girl.hid。
    右向外联接
    语法 select 表1.字段 [as 别名],表n.字段 from 表1 right JOIN 表n on 条件;
    select boy.hid,boy.bname,girl.hid,girl.gname from boy right join girl on boy.hid=girl.hid

  • 相关阅读:
    【数据结构-图】最短路径
    django请求生命周期流程图 路由匹配 无名分组 有名分组 反向解析 无名有名反向解析 路由分发 名称空间
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统功能清单
    云原生Kubernetes系列项目实战-k8s集群+高可用负载均衡层+防火墙
    P4学习——Basic Tunneling
    从零开始完成一副西南地区全图的地图版面设计
    由一个按键程序引发的思考(上)
    spring boot数据导出excel
    45_System类
    QGraphicsView自定义游标【解决修改光标样式不起作用】
  • 原文地址:https://blog.csdn.net/w1404273025/article/details/125912565