Java实训笔记(八)之MYSQL篇 今天首先进行函数的讲解,函数主要分为: 1、 聚合函数 2、 字符串函数 3、 日期函数 4、 数学函数 聚合函数:由一组值计算后产生一个值。 常用函数有: 函数名 函数意义 Count() 统计非空行数 Max()、min() 计算最大数、
Java实训笔记(八)之MYSQL篇
今天首先进行函数的讲解,函数主要分为:
1、 聚合函数
2、 字符串函数
3、 日期函数
4、 数学函数
聚合函数:由一组值计算后产生一个值。
常用函数有:
函数名 |
函数意义 |
Count() |
统计非空行数 |
Max()、min() |
计算最大数、计算最小数 |
Avg() |
求平均 |
Sum() |
求和 |
在函数中还可以使用distinct来表示不重复数据。
问题:计算每门课程的参加考试的人数?(假定score来表示成绩表,此表中有sid表示学号、cid表示课程号、grade表示成绩)
解决问题方法:
1、统计人数使用count;
2、每门课程需要使用分组(group by)
答案:
Select count(*) from score where grade is not null group by cid;
字符串函数:在mysql字符串中第一个字符的编号从1开始。
常用函数有:
函数名 |
函数意义 |
Length()、bit_length() |
求字符串长度、二进制字符串的长度 |
Concat() |
字符串连接 |
Ascii() |
求平均 |
Char |
求和 |
Ltrim() |
去除左边空格 |
Rtrim() |
去除右边空格 |
Left() |
从左边开始取字符 |
Right() |
从右边取开始取字符 |
Instr() |
查找位置 |
Substring() |
取子串 |
Rerverse() |
字符串反转 |
Bin() |
二进制 |
Oct() |
八进制 |
Hex() |
十六进制 |
Locate() |
得到字符的出现的位置 |
Repeat() |
字符重复 |
Format() |
格式字符串 |
日期函数:用来处理日期的函数
1、 ADDDATE(date,INTERVAL expr type) ADDDATE(expr,days) (ADDDATE()就是DATE_ADD()的同义词)
Type值 |
type 值 |
MINUTE_SECOND |
MICROSECOND |
HOUR_MICROSECOND |
SECOND |
HOUR_SECOND |
MINUTE |
HOUR_MINUTE |
HOUR |
DAY_MICROSECOND |
DAY |
DAY_SECOND |
WEEK |
DAY_MINUTE |
MONTH |
DAY_HOUR |
QUARTER |
YEAR_MONTH |
YEAR |
MINUTE_MICROSECOND |
SECOND_MICROSECOND |
例如:推迟一个月
Select adddate(curdate(),interval 1 month);(curdate()获得当前日期,now()获得当前日期和时间,curtime()获得当前时间)
2、 Datediff:两者日期之间的差值
3、 DAY() 或者dayofmonth()
4、 Dayname():
5、 Dayofweek
6、 Dayofyear
7、 Extract():用来提取日期中某一个部分;
问题:统计本周内的营业额.(销售表sale(sid,sname,sdate,count,number));
解决问题方法:如何确定本周,在这里就要用到extract函数用来取出日期中的周数即可。
代码:select sum(count*number) from sale
Where extract(week from curdate()) = extract(week from sdate);
数学函数
Random():用来产生随机数。
多表查询:
实现多表查询主要通过连接和子查询来实现。
连接的方式:连接分为内连接和外连接,外连接分为做外连接和右外连接。
示意图:(通过学生信息表和成绩表,两张表有一个公共字段叫做学号)
代码示例:
三张示例表的结构:学生信息表stdinfo(sid pk,sname,sex ,address),成绩表 score(sid ,cid ,grade,pk(sid,cid)),课程表 course(cid pk,cname)注明:pk表示主键
问题:查询学生的考试成绩。
解决问题方法:根据以上信息需要用到几张表,然后在看获得的信息是否是两张表的共同的值。
代码:select sname,cid,grade from stdinfo s inner join score sc on s.sid = sc.sid;
问题:把对应的课程名称显示出来.
问题:显示所有学生的考试信息。
解决问题的方法:在这里需要用到内连接和左外连接。
子查询:在语句中含有select查询,称为子查询,子查询之外的称为外部查询,先计算子查询,然后在计算外部查询。
语法:select 列名 from 表名 where 。。(子查询)
外部查询可以是update或者delete,他们都有一个共同特点就是都含有where子句,在where中通过运算符与子查询连接,可用的运算符为比较运算符和in。
问题:查找和川大出版社在同一个城市的作者。
解决问题方法:首先要查出川大出版社所在的城市,然后在根据这个城市查找作者。
表结构:author(aid,author_name,sex,birth,city)
Publisher(pid ,publisher_name,city)
代码:select author_name,sex birth ,city from author where
City in (select city from publisher where publisher_name=’川大出版社’);
连接实现方式:
Select author_name,sex,birth,city from author inner join (select city from publisher where publisher_name=’川大出版社’) c on author.city = c .city;

BlobdatatypesinmysqlareusedforvoringLargebinarydatalikeImagesoraudio.1) Useblobtypes (tinyblobtolongblob) Basedondatasizeneeds. 2) Storeblobsin Perplate Petooptimize Performance.3) ConsidersxterNal Storage Forel Blob Romana DatabasesizerIndimprovebackupupe

ToadduserstoMySQLfromthecommandline,loginasroot,thenuseCREATEUSER'username'@'host'IDENTIFIEDBY'password';tocreateanewuser.GrantpermissionswithGRANTALLPRIVILEGESONdatabase.*TO'username'@'host';anduseFLUSHPRIVILEGES;toapplychanges.Alwaysusestrongpasswo

MySQLofferseightstringdatatypes:CHAR,VARCHAR,BINARY,VARBINARY,BLOB,TEXT,ENUM,andSET.1)CHARisfixed-length,idealforconsistentdatalikecountrycodes.2)VARCHARisvariable-length,efficientforvaryingdatalikenames.3)BINARYandVARBINARYstorebinarydata,similartoC

ToaddauserinMySQL,usetheCREATEUSERstatement.1)UseCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';tocreateauser.2)Enforcestrongpasswordpolicieswithvalidate_passwordpluginsettings.3)GrantspecificprivilegesusingGRANTstatement.4)Forremoteaccess,use

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
