第五章 数据查询(二)
5.1 模糊查询 模糊查询提取的数据不一定的确切的,查询者对查询条件也是模糊的、大概的、不特别明确的。例如,只查询张姓学员的信息、只查询分数在60-83分之间的考试成绩、或者只查询北京上海广州地区的学员,这种查询不是指定某个人的姓名、一个具体的分数
5.1 模糊查询
模糊查询提取的数据不一定的确切的,查询者对查询条件也是模糊的、大概的、不特别明确的。例如,只查询张姓学员的信息、只查询分数在60-83分之间的考试成绩、或者只查询北京上海广州地区的学员,这种查询不是指定某个人的姓名、一个具体的分数或者某个固定的地区,因此属于模糊查询。
模糊查询可以使用我们前面已经介绍过的Like、通配符来进行。上一章讲述过的Is Null查询严格说也是一种模糊查询,模糊查询还有基于范围内的查询和在某些列举值内的查询。
5.1.1 使用Like进行模糊查询
在前面我们已经介绍过使用Like来编写约束,Like运算符用于匹配字符串或字符串的一部分(称为子串)。由于该运算符只用于字符串,所以仅与char或varchar数据类型联合使用。
在数据更新、删除或者查询的时候,依然可以使用Like关键字来进行匹配查找,例如,查找姓张的学员:
Select * from Students Where Sname like '张%'
或者查询不是八月份发行的A卡或者C卡:
Select * from Card where ID Like '00[^8]%[A,C]%'
5.1.2 使用BetWeen在某个范围内进行查询
使用关键字Between可以查找那些介于两个已知值之间的一组未知值。要实现这种查找,必须知道开始查找的初值和终值,这个最大值和最小值用单词And分开,例如:
Select * from Score where Score Berween 60 and 80
此外,Between 查询在日期范围的时候使用得比较多。例如,查询不在1992年8月1号到1993年8月1号之间订购的读书列表:
Select * from Sales Where ord_date not Between '1992-8-1' and '1993-8-1'
5.1.3 使用In在列举值内进行查询
查询的值是指定的某些值之一,可以使用带列举值的In关键字来进行查询。将列举值放在圆括号里,用逗号分开,例如查询北京、广州或者上海的学员姓名:
Select Sname as 学员姓名 from Students Where Saddress in ('北京','广州','上海') Order by Saddress
还可以把In关键字和Not关键字合起来使用,这样可以得到所有不匹配列举值的行。
5.2 SQL Server 中的聚合函数
在查询中还会经常碰到的要求是取某些列的最大值、最小值、平均值等信息,有时候还需要计算出究竟查询到多少行数据项。这个时候,查询的“统计数据”是用户比较关心的,SQL Server 提供了“聚合函数”,聚合函数能够基于列进行计算,并返回单个值。
SQL Server 提供了以下几种聚合函数。
1. Sum
Sum返回表达式中所有数值的总和,Sum只能用于数字类型的列,不能够汇总字符、日期等其他数据类型。要得到商务付款的总数,可执行以下查询:
Select Sum(ytd_sales) From titles where type = 'business'
注意这种查询只返回一个数值,因此,不能够直接与可能返回多行的列一起使用来进行查询,例如:
Select Sum(ytd_sales) , Price from titles where type = 'business'
将报告错误信息。但是,在一个查询中可以同时使用多个聚合函数。
2. Avg
Avg函数返回表达式中所有数值的平均值,香港服务器租用,Avg函数也只能用于数字类型的列。例如,成绩表中存在的数据项。
要查询及格线以上的学员的平均成绩,语句如下:
Select Avg(Score) as 平均成绩 from Score Where Score >= 60
3. Max 和 Min
Max返回表达式中的最大值,Min返回表达式中的最小值,它们都可以用于数字型、字符型以及日期/时间类型的列。
例如,查询平均成绩、最高分、最低分的语句如下:
Select Avg(Score) as 平均成绩 , Max(Score) as 最高分, Min(Score) as 最低分 from Score Where Score >= 60
4. Count
Count 返回提供的表达式中非空值的计数,Count可以用于数字和字符类型的列。
另外,也可以使用星号(*)作为Count的表达式,使用星号可以不必指定特定的列而计算所有的行数。
例如,查询及格人数的语句如下:
Select Count(*) as 及格人数 From Score where Score >= 60
5.3 分组查询
5.3.1 使用 Group By 进行分组查询
我们来看一下学员成绩表,成绩表中存储了所有课程的成绩。在这种情况下,可能就需要统计不同课程的平均成绩,也就是说,需要对不同的成绩首先按照课程来进行分组,分组以后再进行聚合计算,得到累计信息。
这种情况应用很普遍,例如一个早餐店,早晨销售包子、油条、米粉等,中午的时候,就需要分类统计包子的销售金额、油条的销售金额、米粉的销售金额,这个时候就需要首先分类,然后在这个类别的基础上分别进行汇总和统计输出。
再具体点,假设学员成绩表中有以下数据。
此时,要统计不同课程的平均分数。首先把相同的CourseID都分为一组,然后把这些相同组对应的分数值再使用前面的聚合函数取平均值(最大值、最小值或者参考人数统计)。

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

AI Hentai Generator
Generate AI Hentai for free.

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



CakePHP is a popular PHP framework that provides convenient ORM (Object Relational Mapping) functionality that makes querying and updating the database very easy. This article will introduce how to query and update data in CakePHP. We'll start with simple queries and updates and work our way up to see how to use conditions and associated models to query and update data more complexly. Basic Query First, let's see how to make the simplest query. Let's say we have a data table called "Users" and we want

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query

MySQL and Python: How to implement data query function In recent years, the rapid growth of data has made data query and analysis an important task in various fields. As a widely used relational database management system, MySQL, combined with Python, a powerful programming language, can provide fast and flexible data query functions. This article will introduce how to use MySQL and Python to implement data query functions and provide code examples. First, we need to install and configure MySQL and Python

Comparison of data query and analysis capabilities between MySQL and TiDB As the amount of data continues to grow and application scenarios become more complex, data query and analysis capabilities have become one of the core competitiveness of various data storage systems. As one of the representatives of relational databases, MySQL has been widely used in stand-alone environments. However, as the business scale continues to expand, MySQL has certain limitations in processing large-scale data and high-concurrency queries. TiDB is an emerging distributed database system that solves these problems.

How to use PHP to implement data-driven and dynamic content generation functions. On many occasions in website development, we often need to dynamically generate content based on data. As a widely used server-side scripting language, PHP provides rich functions and tools to achieve data-driven and dynamic content generation. This article will introduce how to use PHP to implement these functions and provide corresponding code examples. Data-driven Data-driven refers to generating content based on data in databases or other data sources to achieve dynamic display and interaction. PHP provides

How to use MySQL and Ruby to implement a simple data query and analysis function. In today's big data era, data analysis has become an indispensable part in many fields. When performing data analysis, the most common operation is data query. This article will introduce how to use MySQL and Ruby programming language to implement a simple data query and analysis function, and give specific code examples. First, we need to install the MySQL and Ruby development environments. MySQL is an open source relational database

How to use Oracle for data storage and query in Workerman Summary: Workerman is a high-performance PHP development framework that is widely used in real-time communication, instant chat and other Internet applications. Oracle is a powerful relational database that is widely used in enterprise-level applications. This article will introduce how to use Oracle for data storage and query in Workerman, and provide specific code examples. 1. Install Oracle extensions and use Oracle database

How to use PHP database connection to implement data query and update 1. MySQL database connection Before using database connection in PHP, you need to ensure that the MySQL database server has been correctly installed and configured. Next, we will learn how to use PHP to connect to the MySQL database and perform data query and update operations. Install and configure MySQL First, you need to install the MySQL database server. Depending on the operating system, you can choose to use the installation package officially provided by MySQL, or through the integrated development
