search
HomeDatabaseMysql Tutorial模糊查询和聚合函数_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()函数只能用于数字类型的列,不能够汇总字符、日期等其他数据类型。列如:
模糊查询和聚合函数_MySQL

 

--查询全班学生的总成绩

 

select sum(studentresult) as 全班学生的总分 

from result

模糊查询和聚合函数_MySQL

AVG()函数

 

AVG()函数也只能用于数字类型的列。例如:

模糊查询和聚合函数_MySQL

--查询全班学生的平均分

 

select avg(studentresult) as 全班学生的平均分

from result

模糊查询和聚合函数_MySQL

MAX()函数和 MIN()函数

 

MAX()函数是返回值表达式中的最大值,MIN()函数是返回值表达式中的最小值,这两个函数同样都忽略任何空值,并且他们都可以用于数字型、字符串及日期/时间类型的列。对于字符序列,MAX()函数查找排序序列的最大值。而MIN()函数同理,返回排序序列的最小值。例如:         

模糊查询和聚合函数_MySQL

--班级的平均分、最高分和最低分

 

select AVG(Studentresult) as 平均分,MAX(Studentresult) as 最高分,MIN(Studentresult) AS 最低分

from Result

模糊查询和聚合函数_MySQL

 

COUNT()函数

 

COUNT ()函数返回提供的组或记录集中地计数,COUNT()函数可以用于除去 text、image、ntext以外任何类型的列。另外,也可以使用星号(*)作为COUNT的表达式,使用星号可以不必指定特定的列而计算所有的行数,当对所有的行进行计数时,则包括包含空值得行。例如:

模糊查询和聚合函数_MySQL

--记录总数

 

select count(Studentresult) as 总记录数

from result
模糊查询和聚合函数_MySQL


Statement
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
Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

MySQL: How to Add a User RemotelyMySQL: How to Add a User RemotelyMay 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

The Ultimate Guide to MySQL String Data Types: Efficient Data StorageThe Ultimate Guide to MySQL String Data Types: Efficient Data StorageMay 12, 2025 am 12:05 AM

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

MySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMay 11, 2025 am 12:13 AM

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

MySQL: Should I use root user for my product?MySQL: Should I use root user for my product?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL String Data Types Explained: Choosing the Right Type for Your DataMySQL String Data Types Explained: Choosing the Right Type for Your DataMay 11, 2025 am 12:10 AM

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns

See all articles

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 Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.