模糊查询和聚合函数_MySQL
1.使用 LIKE、BETWEEN、IN 进行模糊查询
2.使用聚合函数统计和汇总查询信息
在模糊查询中,我们要学习的内容:
10.1.1 通配符
10.1.2 使用 LIKE 进行模糊查询
10.1.3 使用 BETWEEN 在某个范围内进行查询
10.1.4 使用 IN 在列举值内进行查询
通配符
简单地讲,通配符是一类字符,它可以代替一个或多个真正的字符,查找信息时作为替代字符出现。T-SQL 中的通配符必须
与 LIKE 关键字一起使用,以完成特殊的约束或要求。
通配符表
通 配 符 | 解 释 | 示 例 |
- | 一个字符 | A LIKE 'C_', 则符合条件的 A 如 CS、Cd 等 |
% | 任意长度的字符串 | B LIKE 'CO%', 则符合条件的 B 如 CONST、COKE 等 |
[] | 括号中所指定范围内的一个字符 | C LIKE '9W0[1-2]', 则符合条件的 C 如 9W01 或 9W02 |
[^] |
不在括号中所指定范围内的任 意一个字符 |
D LIKE '9W0[^1-2]', 则符合条件的 D 如 9W03 或 9W07 等 |
使用 LIKE 进行模糊查询
LIKE 运算符用于匹配字符串或字符串的一部分,由于该运算符只用于字符串,所以仅与字符数据类型(如 char 或 varchar 等) 联合使用。在数据更新、删除或者查询的时候,依然可以使用 LIKE 关键字进行匹配查找:
select * from student
where Address like '北京'
使用 BETWEEN 在某个范围内进行查询
使用关键字 BETWEEN 可以查找那些介于两个已知值之间的一组未知值,要实现这种查找,必须知道查找的初值和终值,并且初值要小于终值,初值和终值用 AND 关键字分开。如下:
--查找班级在1-3之间的学生
select * from Student
where GradeId between 1 and 3
使用 IN 在列举值内进行查询
查询的值是指定的某些值之一,可以使用带列举值的 IN 关键字来进行查询。将列举值放在圆括号里,用逗号分开。列如:
--查询地址是北京、上海、厦门的学生
select * from student
where address in('北京','上海','厦门')
在使用聚合函数统计和汇总查询信息的学习中,我们主要学习了四种函数:
10.2.1 SUM()函数
10.2.2 AVG()函数
10.2.3 MAX()函数和 MIN()函数
10.2.4 COUNT()函数
SUM()函数
SUM()函数只能用于数字类型的列,不能够汇总字符、日期等其他数据类型。列如:
--查询全班学生的总成绩
select sum(studentresult) as 全班学生的总分
from result
AVG()函数
AVG()函数也只能用于数字类型的列。例如:
--查询全班学生的平均分
select avg(studentresult) as 全班学生的平均分
from result
MAX()函数和 MIN()函数
MAX()函数是返回值表达式中的最大值,MIN()函数是返回值表达式中的最小值,这两个函数同样都忽略任何空值,并且他们都可以用于数字型、字符串及日期/时间类型的列。对于字符序列,MAX()函数查找排序序列的最大值。而MIN()函数同理,返回排序序列的最小值。例如:
--班级的平均分、最高分和最低分
select AVG(Studentresult) as 平均分,MAX(Studentresult) as 最高分,MIN(Studentresult) AS 最低分
from Result
COUNT()函数
COUNT ()函数返回提供的组或记录集中地计数,COUNT()函数可以用于除去 text、image、ntext以外任何类型的列。另外,也可以使用星号(*)作为COUNT的表达式,使用星号可以不必指定特定的列而计算所有的行数,当对所有的行进行计数时,则包括包含空值得行。例如:
--记录总数
select count(Studentresult) as 总记录数
from result

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Go language provides two dynamic function creation technologies: closure and reflection. closures allow access to variables within the closure scope, and reflection can create new functions using the FuncOf function. These technologies are useful in customizing HTTP routers, implementing highly customizable systems, and building pluggable components.

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

1. The SUM function is used to sum the numbers in a column or a group of cells, for example: =SUM(A1:J10). 2. The AVERAGE function is used to calculate the average of the numbers in a column or a group of cells, for example: =AVERAGE(A1:A10). 3. COUNT function, used to count the number of numbers or text in a column or a group of cells, for example: =COUNT(A1:A10) 4. IF function, used to make logical judgments based on specified conditions and return the corresponding result.

The advantages of default parameters in C++ functions include simplifying calls, enhancing readability, and avoiding errors. The disadvantages are limited flexibility and naming restrictions. Advantages of variadic parameters include unlimited flexibility and dynamic binding. Disadvantages include greater complexity, implicit type conversions, and difficulty in debugging.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The key to writing efficient and maintainable Java functions is: keep it simple. Use meaningful naming. Handle special situations. Use appropriate visibility.

The difference between custom PHP functions and predefined functions is: Scope: Custom functions are limited to the scope of their definition, while predefined functions are accessible throughout the script. How to define: Custom functions are defined using the function keyword, while predefined functions are defined by the PHP kernel. Parameter passing: Custom functions receive parameters, while predefined functions may not require parameters. Extensibility: Custom functions can be created as needed, while predefined functions are built-in and cannot be modified.

Exception handling in C++ can be enhanced through custom exception classes that provide specific error messages, contextual information, and perform custom actions based on the error type. Define an exception class inherited from std::exception to provide specific error information. Use the throw keyword to throw a custom exception. Use dynamic_cast in a try-catch block to convert the caught exception to a custom exception type. In the actual case, the open_file function throws a FileNotFoundException exception. Catching and handling the exception can provide a more specific error message.
