Home Database Mysql Tutorial Java实训笔记(八)之mysql

Java实训笔记(八)之mysql

Jun 07, 2016 pm 03:13 PM
java mysql notes

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():用来产生随机数。

 

多表查询:

实现多表查询主要通过连接和子查询来实现。

连接的方式:连接分为内连接和外连接,外连接分为做外连接和右外连接。

示意图:(通过学生信息表和成绩表,两张表有一个公共字段叫做学号)

Java实训笔记(八)之mysql

 

Java实训笔记(八)之mysql

Java实训笔记(八)之mysql

Java实训笔记(八)之mysql

代码示例:

三张示例表的结构:学生信息表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;

Java实训笔记(八)之mysql

问题:把对应的课程名称显示出来.

 

Java实训笔记(八)之mysql

问题:显示所有学生的考试信息。

解决问题的方法:在这里需要用到内连接和左外连接。

Java实训笔记(八)之mysql

 

子查询:在语句中含有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;

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

SQL vs. MySQL: Clarifying the Relationship Between the Two SQL vs. MySQL: Clarifying the Relationship Between the Two Apr 24, 2025 am 12:02 AM

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

What does 'platform independence' mean in the context of Java? What does 'platform independence' mean in the context of Java? Apr 23, 2025 am 12:05 AM

Java's platform independence means that the code written can run on any platform with JVM installed without modification. 1) Java source code is compiled into bytecode, 2) Bytecode is interpreted and executed by the JVM, 3) The JVM provides memory management and garbage collection functions to ensure that the program runs on different operating systems.

See all articles