MySql数据库---子查询,多表连接查询,自连接查询,串联查询,数学函数,字符串函数,时间日期函数,判断分支语句

思维导图

子查询[分步走]

1:一个sql的查询结果当做另一个sql的查询条件.
2:内层的那个sql语句要先执行

-- todo --------------子查询---(嵌套查询)---------------
-- 例如,使用命令完成:
-- (1)使用数据库班级db_product3下的商品表和分类表来操作;
-- (2)查询商品表、分类表的所有数据信息;
select * from tb_product;
select * from tb_category;
-- (3)查询分类为"服装"的所有商品信息;[分类id、商品]
select cid from tb_category where cname = '服装';
select * from tb_product where cid = (select cid from tb_category where cname = '服装');
​
-- (4)查询商品名称为"格力"的分类信息;
select cid from tb_product where name = '格力';
select cname from tb_category where cid = (select cid from tb_product where name = '格力');
​
-- (5) 查询商品价格高于平均价格的商品信息
select * from tb_product where price > (select avg(price) from tb_product);

交叉连接[了解]

-- todo 交叉连接---笛卡尔积查询----[了解]---------
# 语法1:
select * from table_category,table_product;
# 语法2:
select * from table_product cross join table_category;

内连接

查询的是两个表的交集.

-- todo 内连接查询 --[掌握]--------------
# 例如,使用内连接查询命令完成:
# (1)使用隐式内连接查询类别表和商品表的共有数据信息;
# select * from table_category,table_product where table_category.cid = table_product.category_id;
select * from table_product tp,table_category tc where tc.cid = tp.category_id;-- 使用别名
# (2)使用显式内连接来查询类别表和商品表的公共商品信息;
select * from table_category tc inner join table_product tp on tc.cid = tp.category_id;
select * from table_category tc  join table_product tp on tc.cid = tp.category_id; -- 省略inner

左外连接

左连接查询的是:左表的全部和右边表能连接上的数据.
左边特有的数据:右边没有的值用null填充
# -- todo 左外连接查询-----------左边的全部和右表中能关联的数据.
# 例如,使用左外连接查询命令完成:
# (1)以左表为主,连接查询类别表和商品表中的所有商品数据信息
select * from table_category tc left join table_product tp on tc.cid = tp.category_id;
#(2)以左表为主,连接查询商品表和类别表中的所有商品数据信息
select * from table_product tp left join table_category tc  on tc.cid = tp.category_id;

右连接[了解]

右连接查询的是:右表的全部和左边表能连接上的数据.
右边特有的数据:左边没有的值用null填充

# -- todo 右外连接查询-----------右边的全部和左表中能关联的数据.[了解]
select * from table_category tc right join table_product tp on tc.cid = tp.category_id;

自连接查询

create table area(pid int primary key auto_increment,name varchar(20),city_id int
);
​
insert into area values (1,'广东省',null),(2,'江西省',null),(3,'广州市',1),(4,'深圳',1),(5,'东莞',1),(6,'南昌',2),(7,'赣州',2),(8,'九江',2);
​
# 例如,使用自连接查询命令完成:
# (1)把区域表tb_area分别理解成两个表:省表province、城市表city;
# (2)自连接查询省份编号、省名、城市名、城市编号的展示结果;
select province.pid,province.name,city.name,city.pid from area province join area city on province.pid = city.city_id;
# (3)自连接查询省的名称为广东省的所有城市信息。
select a.pid,a.name,b.name,b.pid from area a join area b on a.pid = b.city_id where a.name = '广东省';

子查询的三种情况

1.子查询当条件(单值对比)
2.子查询当条件(多值对比)
3.子查询当临时表(出现在from的后边位置)
-- todo 子查询三种情况--------------------
# 1.子查询当条件(单值对比--where 后边)
-- 查询 工资高于平均工资的员工信息
select * from emp where sal > (select avg(sal) from emp);
​
# 2.子查询当条件(多值对比)
-- 查询销售部和财务部所有员工信息.
select * from emp where deptno in (select deptno from dept where dname = '销售部' or dname='财务部');
select * from emp where deptno in (select deptno from dept where dname in ('销售部','财务部'));
select e.* from emp e join dept d on e.deptno = d.deptno where dname = '销售部' or dname='财务部';-- 连接查询实现
​
# 3.子查询当临时表(出现在from的后边位置)
-- 查询工资高于15000元的员工信息和他的部门信息
select * from emp where sal > 15000;
select d.dname,d.loc,e.* from dept d join (select * from emp where sal > 15000) e on d.deptno = e.deptno;

union连接查询

union: 纵向拼接去重
union all:纵向拼接不去重
-- 需求:查询工资大于28000或者部门是10号部门的员工信息.
select * from emp where sal > 28000
union
select * from emp where deptno = 10;
​
select * from emp where sal > 28000
union all
select * from emp where deptno = 10;
​
-- 两个表列和类型相同但是名字不同也可以拼接.
create table teacher(t_id int,t_name varchar(20),t_gender varchar(20)
);
​
create table student(s_id int,s_name varchar(20),s_gender varchar(20)
);
​
insert into teacher values (1,'张三','男'),(2,'李四','男'),(3,'王五','男'),(4,'小美','女');
insert into student values (1,'tom','男'),(2,'jerry','男'),(3,'jack','男'),(4,'rose','女');
​
-- 需求 查询男性的学生和老师.
select t_id id,t_name name,t_gender gender from teacher where t_gender = '男'
union
select * from student where s_gender = '男';

数学函数

-- todo mysql 数学类函数----------------
-- round -- 指定小数位(四舍五入)
select round(3.1415926); -- 3
select round(3.1415926,2); -- 3.14
select round(3.145,2); -- 3.15 -- 四舍五入
​
-- format 格式化数字展示便于阅读,指定小数位(四舍五入)
select format(12345.1415926,3);
​
-- floor -- 舍弃小数位-- 向下取整
select floor(12345.141592);-- 12345
select floor(12345.541592);-- 12345
select floor(12345.941592);-- 12345
-- ceil -- 向上取整
select ceil(12345.141592);-- 12346
select ceil(12345.541592);-- 12346
select ceil(12345.941592);-- 12346
​
-- mod 模运算 -- (求余数)
select mod(5,3); -- 2
​
-- pow(x,y) x的y 次方
select pow(2,3); -- 8
​
-- rand() 随机数函数
select rand();-- 0-1之间的随机数 -- 每次都是变化的.
select rand(10);-- 根据种子生成随机数.每次都是固定的.

字符串函数

-- todo 字符串相关的函数----------------------------
select upper('hello');
select lower('HELLO');
select s_id,upper(student.s_name),s_gender from student;
-- 第一个参数是要操作的字符串(列),要替换的字符,提换成xx字符.
select replace('黑马程序员','黑马','白马');
​
-- 把所有参数(n个)拼接成为一个大的字符串.不可拼接null
select concat('a','黑','b','白',100,false,true);
​
-- 按照指定字符把多个参数进行拼接.最后成为一大的字符串
select concat_ws('_','a','b','中文');-- 拼接成为二维数据--csv格式的数据.
​
-- 重复字符串拼接--指定次数
select repeat('我错了',3);
​
-- 把字符串内容倒序输出
select reverse('abc');
select reverse('你好吗');
​
-- substr 按照位置截取指定个数的字符串
select substr('hello',2);-- ello
select substr('hello',1,1);-- h
select substr('hello',-2,2);-- lo
select substr('hello',-5,5);-- hello
​
select left('hello',3);-- 从左侧截取制定个数的字符串
select right('hello',3);-- 从右侧截取制定个数的字符串
​
select char_length('abc');-- 求字符串长度
select char_length('你好');
select length('abc');-- 推荐这个 --求字符串长度
​
-- 需求 : 把学生的名字首字母转大写其它不变.
select * from student;
select concat(upper(substr(student.s_name,1,1)),substr(s_name,2)) from student;

时间日期函数

-- todo 时间日期相关函数----------------------
select now();-- 2024-09-25 15:56:31
select current_date();-- 2024-09-25
select current_time();-- 15:57:15
​
-- 第一个时间要大于第二个时间.求的是第一参减去第二参的时间差(单位是天)
select datediff('2023-09-18','2022-09-10');-- 8
​
-- 加时间
select date_add(now(),INTERVAL 1 DAY);
select date_add(now(),INTERVAL -1 DAY);-- 加负数就是减
select date_add(now(),INTERVAL 10 YEAR);
​
-- 时间减法
select date_sub('2000-10-11',interval 1 day);-- 2000-10-10
select date_sub(20001010,interval 1 day);-- 2000-10-09
​
-- 把第二个时间添加到第一个时间上并返回.
select timestamp(now(),'10:10:10');
​
select YEAR(now());-- 单独获取年
select MONTH(now());-- 单独获取月
select day(now());-- 单独获取日
select hour(now());-- 单独获取时
​
-- 使用格式化指定时间格式
select date_format(now(),'%Y年%m月%d日 %H:%i:%s');-- 2024年09月25日 16:16:35
select date_format(now(),'%Y-%m-%d %H:%i:%s');-- 2024-09-25 16:16:30
select date_format(now(),'%Y/%m/%d %H:%i:%s');-- 2024/09/25 16:16:24
​
-- 把年月日变成秒值.
select unix_timestamp();-- 1727252256
select unix_timestamp(now());-- 1727252256
select unix_timestamp('1970-01-01');-- 1727252256 -- 有时候数据库存储的时间就是秒值
​
-- 把秒值时间抓换为年月日时间
select from_unixtime(0);
select from_unixtime(1727252256);
​
select '2020-10-10' > '2020-10-09'; -- 底层是转秒值然后对比.

时间日期案例

-- ------sql日期案例------2020年最后一次登录------------------------------------------------------------
Create table If Not Exists Logins (user_id int, time_stamp datetime);
Truncate table Logins;
insert into Logins (user_id, time_stamp) values ('6', '2020-06-30 15:06:07');
insert into Logins (user_id, time_stamp) values ('6', '2021-04-21 14:06:06');
insert into Logins (user_id, time_stamp) values ('6', '2019-03-07 00:18:15');
insert into Logins (user_id, time_stamp) values ('8', '2020-02-01 05:10:53');
insert into Logins (user_id, time_stamp) values ('8', '2020-12-30 00:46:50');
insert into Logins (user_id, time_stamp) values ('2', '2020-01-16 02:49:50');
insert into Logins (user_id, time_stamp) values ('2', '2019-08-25 07:59:08');
insert into Logins (user_id, time_stamp) values ('14', '2019-07-14 09:00:00');
insert into Logins (user_id, time_stamp) values ('14', '2021-01-06 11:59:59');
-- select * from logins where time_stamp > '2020-01-01' and time_stamp < '2020-12-31';
select user_id,max(time_stamp) from logins where year(time_stamp) = 2020 group by user_id;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/54818.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html TAB、table生成

1. 代码 <!DOCTYPE html> <head> <meta charset"UTF-8"> <title>Dynamic Tabs with Table Data</title> <style> /* 简单的样式 */ .tab-content { display: none; border: 10px solid #ccc; padding: 30px; mar…

音视频生态下Unity3D和虚幻引擎(Unreal Engine)的区别

技术背景 好多开发者跟我们做技术交流的时候&#xff0c;会问我们&#xff0c;为什么有Unity3D的RTMP|RTSP播放模块&#xff0c;还有RTMP推送和轻量级RTSP服务模块&#xff0c;为什么不去支持虚幻引擎&#xff1f;二者区别在哪里&#xff1f;本文就Unity3D和虚幻引擎之间的差异…

Mac使用gradle编译springboot-2.7.x源码

1 开发环境&#xff1a; JDK8 ideaIU-2024.2.2 gradle-7.6.3 代理网络 2 下载springboot源码 代码仓库网址 git clone -b 2.7.x https://github.com/spring-projects/spring-boot.git3 安装gradle gradle下载网址 https://services.gradle.org/distributions/ 安装此文件指…

统信服务器操作系统【刻录镜像制作U盘启动盘的工具】

统信服务器操作系统各版本上刻录镜像制作U盘启动盘的工具方案 文章目录 应用场景一、问题现象二、问题分析解决方案应用场景 硬件/整机信息:全平台 CPU架构:全架构 OS版本信息:服务器a版,e版,d版(其中d版遇到的刻录类问题较少) 软件信息:dd工具、Fedora Media Writer工…

react hooks--useCallback

概述 useCallback缓存的是一个函数&#xff0c;主要用于性能优化!!! 基本用法 如何进行性能的优化呢&#xff1f; useCallback会返回一个函数的 memoized&#xff08;记忆的&#xff09; 值&#xff1b;在依赖不变的情况下&#xff0c;多次定义的时候&#xff0c;返回的值是…

不在同一局域网怎么远程桌面?非局域网环境下,实现远程桌面访问的5个方法分享!

非局域网环境下&#xff0c;怎么远程桌面&#xff1f;还能做到吗&#xff1f; 在企业管理中&#xff0c;远程桌面访问已成为提高工作效率、实现跨地域协同工作的关键工具。 然而&#xff0c;当被控端与控制端不在同一局域网时&#xff0c;如何实现远程桌面连接成为了许多企业…

时序预测:LSTM、ARIMA、Holt-Winters、SARIMA模型的分析与比较

引言 近年来&#xff0c;民航旅客周转量一直是衡量国家或地区民航运输总量的重要指标之一。为了揭示民航旅客周转量背后的规律和趋势&#xff0c;本研究旨在综合分析1990年至2023年的相关数据。 通过单位根检验和序列分解&#xff0c;我们确定了民航旅客周转量数据的非平稳性&…

《大学编译原理:语言翻译的艺术与科学》

在大学的计算机科学课程中&#xff0c;编译原理无疑是一门充满挑战与魅力的重要学科。它就像是一座连接高级编程语言和计算机硬件的桥梁&#xff0c;让程序员能够用人类易于理解的语言编写代码&#xff0c;而计算机则能高效地执行这些指令。 一、编译原理的重要性 编译原理是…

Android个性名片界面的设计——约束布局的应用

节选自《Android应用开发项目式教程》&#xff0c;机械工业出版社&#xff0c;2024年7月出版 做最简单的安卓入门教程&#xff0c;手把手视频、代码、答疑全配齐 【任务目标】 使用约束布局、TextView控件实现一个个性名片界面的设计&#xff0c;界面如图1所示。 图1 个性名片…

C++之 string(中)

C之 string string类对象的容量操作 resize 将有效字符的个数该成n个&#xff0c;多出的空间用字符c填充 虽然在string里用的不多&#xff0c;但是在vector里面常见 这里有三种情况&#xff1a; 1&#xff09;resize小于当前的size 2)resize大于当前的size,小于capacity …

计算机视觉的应用34-基于CV领域的人脸关键点特征智能提取的技术方法

大家好&#xff0c;我是微学AI&#xff0c;今天给大家介绍一下计算机视觉的应用34-基于CV领域的人脸关键点特征智能提取的技术方法。本文主要探讨计算机视觉领域中人脸关键点特征智能提取的技术方法。详细介绍了基于卷积神经网络模型进行人脸关键点提取的过程&#xff0c;包括使…

一、Kafka入门

一、消息中间件 1、为什么使用消息中间件&#xff1f; 异步解耦削峰填谷 2、异步 3、解耦 异步处理使整个数据操作流程解耦&#xff0c;如果短信服务或者积分服务异常&#xff0c;不影响前面重要的功能。 面试问题点&#xff1a; 所以尽量将不重要的服务放到下游&#xf…

闲盒支持的组网方式和注意事项

1. 直连光猫拨号​ 通过光猫拨号&#xff0c;设备直连光猫的设备&#xff0c;需要对光猫开启UPNP并关闭DMZ 如果只接一个盒子&#xff0c;建议直接针对盒子IP开dmz。 2. 直连路由器​ 通过路由器拨号&#xff0c;设备直连路由器的设备&#xff0c;需要对路由器开启UPNP并关闭…

Spring Security学习

系列文章目录 第一章 基础知识、数据类型学习 第二章 万年历项目 第三章 代码逻辑训练习题 第四章 方法、数组学习 第五章 图书管理系统项目 第六章 面向对象编程&#xff1a;封装、继承、多态学习 第七章 封装继承多态习题 第八章 常用类、包装类、异常处理机制学习 第九章 集…

如何防止U盘资料被复制?这六个策略你值得牢记!

随着U盘广泛应用于企业和个人数据存储&#xff0c;如何防止U盘资料被非法复制和泄露成为企业信息安全的重要问题。 U盘作为便携的数据存储设备&#xff0c;虽然方便&#xff0c;但也带来了数据泄露的风险。 为了有效防止U盘资料被复制&#xff0c;以下六个策略能够帮助企业和个…

gradio交互式界面部署

gradio交互式界面部署 示例&#xff1a;http://xxxxx:1111/api/v1/my_model 为模型服务api&#xff0c;传入参数为&#xff1a; {"company": company,"name": name,"t_date": t_date,"amount": amount,"img1_path": img1_…

LeetCode Hot100 C++ 哈希 128.最长连续序列

128.最长连续序列 给定一个未排序的整数数组 nums &#xff0c;找出数字连续的最长序列&#xff08;不要求序列元素在原数组中连续&#xff09;的长度。 给定一个未排序的整数数组 nums &#xff0c;找出数字连续的最长序列&#xff08;不要求序列元素在原数组中连续&#xff0…

怎么测试射频芯片质量的好坏?

无论是手机通信&#xff0c;还是卫星导航&#xff0c;射频芯片都是其核心组件之一。本文将探讨如何准确判断射频芯片的质量&#xff0c;以确保技术设备的稳定运行。 1. 外观检查 检查射频芯片是否有破损、引脚断裂、缺陷等。 2. 电气参数测试 对射频芯片的输入输出阻抗、功耗、…

RabbitMQ:交换机详解(Fanout交换机、Direct交换机、Topic交换机)

♥️作者&#xff1a;小宋1021 &#x1f935;‍♂️个人主页&#xff1a;小宋1021主页 ♥️坚持分析平时学习到的项目以及学习到的软件开发知识&#xff0c;和大家一起努力呀&#xff01;&#xff01;&#xff01; &#x1f388;&#x1f388;加油&#xff01; 加油&#xff01…

伊犁云计算22-1 rhel8 dhcp 配置

1 局域网搭建 2 yum 配置 这个参考前面 不说 3 dnf 安装dhcp 好我们废话不说开始安装。理论看书去 进入 dhcp.conf 配置 重启dhcpd 不能报错&#xff01;&#xff01;&#xff01;&#xff01; 我们在客户机上做测试 全局的dhcp关闭 很明显我们的客户机获取到192.16…