当前位置: 首页 > news >正文

linux 下连接mysql(下)

case 表达式

表t1中的数据如下。

select * from t1;
+----+------------+------+------+
| id | student_no | name | age  |
+----+------------+------+------+
|  3 |     202501 | ll   |   10 |
|  4 |     202502 | tt   |   15 |
+----+------------+------+------+

如果学号是202501,则输出hello,其余的值输出mysql。

select case student_no	
       when 202501 then 'hello' 
       else 'mysql' 
       end from t1;
+-----------------------------------------------------------+
| case student_no when 202501 then 'hello' else 'mysql' end |
+-----------------------------------------------------------+
| hello                                                     |
| mysql                                                     |
+-----------------------------------------------------------+

如果年龄大于等于15,输出>=15,年龄在10到15之间,输出10<=age<=15,其余输出<10.

 select case
        when age >= 15 then '>=15' 
        when age between 10 and 15 then '10<= age <=15' 
        else '<10' end  as res from t1;
+---------------+
| res           |
+---------------+
| 10<= age <=15 |
| >=15           |
+---------------+
IF 表达式

格式: IF(表达式1, 表达式2, 表达式3)
如果表达式1的结果为真,则返回表达式2的值,如果表达式1的结果为假,则返回表达式3的值.

 select if(10 < 12, 12 , 15),if(20 < 12, 12, 15);
+---------------------+-------------------+
| if(10< 12, 12 , 15) | if(20 < 12,12,15) |
+---------------------+-------------------+
|                  12 |                15 |
+---------------------+-------------------+
IFNULL()

如果表达式1的值为NULL,则返回表达式2的值。否则使用表达式1的值.

select ifnull(null,'hello'),ifnull(1,'hello');
+----------------------+-------------------+
| ifnull(null,'hello') | ifnull(1,'hello') |
+----------------------+-------------------+
| hello                | 1                 |
+----------------------+-------------------+
NULLIF()

如果表达式1表达式2的结果相同,返回null.否者返回表达式1

 select nullif(1,1),nullif(1,2);
+-------------+-------------+
| nullif(1,1) | nullif(1,2) |
+-------------+-------------+
|        NULL |           1 |
+-------------+-------------+
汇总函数

汇总函数的作用是将某个字段的所有行的数据当作参数,生成一个返回值.

#t2表中的数据
select * from t2;
+----+------------+-------+---------+
| id | student_no | score | subject |
+----+------------+-------+---------+
|  2 |     202501 |   100 | 语文    |
|  3 |     202501 |    90 | 英语    |
|  4 |     202501 |    95 | 数学    |
|  5 |     202502 |    80 | 语文    |
|  6 |     202502 |   100 | 英语    |
|  7 |     202502 |    90 | 数学    |
+----+------------+-------+---------+

#输出所有成绩的最高,最低分和平均分 
select max(score),min(score),avg(score) from t2;
+------------+------------+------------+
| max(score) | min(score) | avg(score) |
+------------+------------+------------+
|        100 |         80 |    92.5000 |
+------------+------------+------------+
分组查询

分组的作用是:将数据以指定的字段分称多个组,在分组内执行汇总函数.
下面语句输出每个同学的最高分,最低分和平均分.

 select student_no,max(score),min(score),avg(score)
 from t2 group by student_no;
+------------+------------+------------+------------+
| student_no | max(score) | min(score) | avg(score) |
+------------+------------+------------+------------+
|     202501 |        100 |         90 |    95.0000 |
|     202502 |        100 |         80 |    90.0000 |
+------------+------------+------------+------------+

如果要对分组后的数据进行过滤,需要使用HAVING.
下面只显示平均分大于90的同学的最高分,最低分和平均分.

select student_no,max(score),min(score),avg(score) from t2 group by student_no having avg(score) > 90;
+------------+------------+------------+------------+
| student_no | max(score) | min(score) | avg(score) |
+------------+------------+------------+------------+
|     202501 |        100 |         90 |    95.0000 |
+------------+------------+------------+------------+

havingwhere的区别:

where 是在分组之前对所有的数据进行过滤.
having 是在分组后,对分组进行过滤.

使用分组时,在查询列表中只能放置分组列和汇总函数

连接查询

连接查询分为内连接外连接.
外连接又分为左连接右连接.

inner join

下面语句查询每个同学的成绩

select t1.student_no, t1.name,t2.subject,t2.score from t1 
inner join t2
on t1.student_no=t2.student_no;
+------------+--------+---------+-------+
| student_no | name   | subject | score |
+------------+--------+---------+-------+
|     202501 | 小明   | 语文    |   100 |
|     202501 | 小明   | 英语    |    90 |
|     202501 | 小明   | 数学    |    95 |
|     202502 | 小华   | 语文    |    80 |
|     202502 | 小华   | 英语    |   100 |
|     202502 | 小华   | 数学    |    90 |
+------------+--------+---------+-------+
左连接

下面同样是查询每个同学的成绩,不同的是将inner join 改为 left join

select t1.student_no, t1.name,t2.subject,t2.score from t1
left join t2 
on t1.student_no=t2.student_no;
+------------+--------+---------+-------+
| student_no | name   | subject | score |
+------------+--------+---------+-------+
|     202501 | 小明   | 语文    |   100 |
|     202501 | 小明   | 英语    |    90 |
|     202501 | 小明   | 数学    |    95 |
|     202502 | 小华   | 语文    |    80 |
|     202502 | 小华   | 英语    |   100 |
|     202502 | 小华   | 数学    |    90 |
|     202503 | 小刚   | NULL    |  NULL |
+------------+--------+---------+-------+

内连接左连接右连接的区别:

内连接:在两张表中找不到匹配的记录,则该记录不会放到结果集中
左连接:在两张表中找不到匹配的记录,左侧表的记录仍需放到结果集中,取不到值的字段显示为NULL.
右连接和左连接正好相反,在两张表中找不到匹配的记录,右侧表的记录仍需放到结果集中,取不到值的字段显示为NULL.

并集查询

并集查询使用UNIONUNION ALL.
UNINOUNION ALL的区别时 UNION 会自动去重.
下面语句查询小明和小华的成绩:

 select score from t2 inner join t1 on t2.student_no=t1.student_no and t1.name = '小明'
    union
    select score from t2 inner join t1 on t2.student_no=t1.student_no and t1.name = '小华';
+-------+
| score |
+-------+
|   100 |
|    90 |
|    95 |
|    80 |
+-------+

select score from t2 inner join t1 on t2.student_no=t1.student_no and t1.name = '小明'
union all
select score from t2 inner join t1 on t2.student_no=t1.student_no and t1.name = '小华';
+-------+
| score |
+-------+
|   100 |
|    90 |
|    95 |
|    80 |
|   100 |
|    90 |
+-------+
数据的插入、更新和删除
插入数据

insert into 表名(field1,field2…) values(val1,val2…), (val1,val2…) …;
下面语句插入小刚的成绩

insert into t2(student_no,subject,score) values(202503,'语文',100),(202503,'数学',100);

##查询结果
select t1.student_no,t1.name,t2.subject,t2.score from t1 
inner join t2 on t1.student_no=t2.student_no
and t1.name = '小刚';
+------------+--------+---------+-------+
| student_no | name   | subject | score |
+------------+--------+---------+-------+
|     202503 | 小刚   | 语文    |   100 |
|     202503 | 小刚   | 数学    |   100 |
+------------+--------+---------+-------+
插入某个查询的结果集

insert into 表名(field1,field2…) select …;

下面语句插入小刚的英语成绩.

insert into t2(student_no,subject.score) select 202503,'英语',99;

select t1.student_no,t1.name,t2.subject,t2.score from t1 inner join t2 on t1.student_no=t2.student_no and t1.name = '小刚';
+------------+--------+---------+-------+
| student_no | name   | subject | score |
+------------+--------+---------+-------+
|     202503 | 小刚   | 语文    |   100 |
|     202503 | 小刚   | 数学    |   100 |
|     202503 | 小刚   | 英语    |    99 |
+------------+--------+---------+-------+
更新数据

update 表名 set … [where …];

下面语句将小刚的英语成绩改为95:

update t2 set score = 95 where student_no=202503 and subject='英语';

select * from t2 where student_no=202503;
+----+------------+-------+---------+
| id | student_no | score | subject |
+----+------------+-------+---------+
|  8 |     202503 |   100 | 语文    |
|  9 |     202503 |   100 | 数学    |
| 10 |     202503 |    95 | 英语    |
+----+------------+-------+---------+
删除数据

delete from 表名 [where …]

下面语句删除小刚的英语成绩


mysql> delete from t2 where student_no=202503 and subject='英语';

mysql> select * from t2 where student_no=202503;
+----+------------+-------+---------+
| id | student_no | score | subject |
+----+------------+-------+---------+
|  8 |     202503 |   100 | 语文    |
|  9 |     202503 |   100 | 数学    |
+----+------------+-------+---------+
视图

视图就是查询语句的一个别名,它不会存储查询的结果.
使用视图的时候,可以把它当作一张表使用.
格式:

create view 视图名 as select …

下面语句创建视图,用于查询所有同学的英语成绩.

create view v_english as  select student_no,score from t2 where subject='英语';

 select * from v_english;
+------------+-------+
| student_no | score |
+------------+-------+
|     202501 |    90 |
|     202502 |   100 |
+------------+-------+
# 删除视图
drop view v_english;

存储过程

格式:

  create procedure 名称([参数列表])
  begin
    #需要执行的语句
  end

存储过程调用:

  call 名称([参数列表])

下面语句输出指定同学的英语成绩

delimiter $

create procedure p_english_score(number int)
begin
    select * from t2 where t2.subject='英语' and t2.student_no=number;
end $

delimiter ;

call p_english_score(202501);
+----+------------+-------+---------+
| id | student_no | score | subject |
+----+------------+-------+---------+
|  3 |     202501 |    90 | 英语    |
+----+------------+-------+---------+

存储函数

格式:

create function 函数名([参数列表])
returns 返回类型
begin
  函数体;
end

下面语句创建函数,用于计算指定学号对应同学的平均分.

delimiter $
create function avg_score(number int) 
returns int 
begin 
  declare res int;
  set res = (select avg(score) from t2 where student_no=number); return res ;
end $
delimiter ;

#使用函数
select avg_score(202501),avg_score(202502),avg_score(202503);
+-------------------+-------------------+-------------------+
| avg_score(202501) | avg_score(202502) | avg_score(202503) |
+-------------------+-------------------+-------------------+
|                95 |                90 |               100 |
+-------------------+-------------------+-------------------+

#删除函数
drop function avg_score;
使用函数计算斐波那契数列
delimiter $
create function fibonacci(n int)
returns int
begin
    declare a,b,c int default 1;
    declare i int default 2;
    set c=2;
    if n <= 2 then
        return a;
    end if;

    label1: while 1
    do
        set c = a + b, a = b, b = c;
        set i = i+1;
        if i >= n then
            leave label1;
        end if;
    end while label1;
    return c;
end $
delimiter ;

select fibonacci(5),fibonacci(6),fibonacci(7);
+--------------+--------------+--------------+
| fibonacci(5) | fibonacci(6) | fibonacci(7) |
+--------------+--------------+--------------+
|            5 |            8 |           13 |
+--------------+--------------+--------------+
存储过程和存储函数的不同点
  • 存储函数在定义时需要显示使用returns语句表明返回的数据类型,函数体中必须使用return语句来指定返回值
  • 存储函数不支持IN、OUT、INOUT的参数前缀
  • 存储函数只能返回一个值,存储过程可以通过OUT和INOUT参数返回多个结果
  • 存储函数执行过程中产生的结果集不会显示到客户端,存储过程产生的结果集会显示到客户端
  • 存储函数直接以函数调用的形式进行调用,存储过程只能通过CALL语句显示调用
用户权限
创建用户

格式:

create user '用户名'@'localhost' identified by '密码';
修改密码
alter user '用户名'@'localhost' identified by '密码';
删除用户
  drop user '用户名'@'localhost';
授予权限

grant 权限名称 on 应用级别 to ‘用户名’@‘localhost’;

应用级别:

  • *.*代表全局级别,作用与任何数据库下的任何对象
  • 数据库.*代表数据库级别,作用于数据库下的任何对象

相关文章:

  • [笔记] 汇编杂记(持续更新)
  • 在freertos中,中断优先级和任务优先级之间的关系和使用方法
  • 在Windows 7操作系统,基于llama.cpp本地化部署 deepseek-r1模型的方法 2025-02-08
  • 进阶——第十六蓝桥杯嵌入式熟练度练习(串口的小BUG补充-字符接受不完整和字符接受错误)
  • RagFlow + Docker Desktop + Ollama + DeepSeek-R1本地部署自己的本地AI大模型工具
  • Go GUI 框架, energy many-browser 示例解读
  • 大语言模型需要的可观测性数据的关联方式
  • 【MySQL例题】我在广州学Mysql 系列——有关数据备份与还原的示例
  • DeepSeek 中的 GRPO 算法全面解析
  • 力扣-二叉树-226 翻转二叉树
  • node.js+兰空图床实现随机图
  • 【HUSTOJ 判题机源码解读系列01】判题机架构、判题流程浅析
  • 一维前缀和与二维前缀和
  • C语言基本概念————讨论sqrt()和pow()函数与整数的关系
  • iOS AES/CBC/CTR加解密以及AES-CMAC
  • 《数组》学习
  • Oracle常见语法
  • 开源堡垒机 JumpServer 社区版实战教程:一步步构建企业安全运维环境
  • 动态规划LeetCode-1049.最后一块石头的重量Ⅱ
  • GESP2024年9月认证C++七级( 第三部分编程题(1)小杨寻宝)
  • 公司注册的公司/seo怎么发布外链
  • 家具网站建设策划书/域名收录查询
  • 用wordpress还是php/seo招聘要求
  • 动态网站开发相关技术/百度推广优化方案
  • 做网页局域网站点配置/哪些网站可以seo
  • 做网站的硬件/代刷网站推广链接免费