mysql 如何对表排序

In this article, we will learn how we can sort and filter data using the WHERE clause and sort the data using the ORDER BY clause. In my previous article, Learn MySQL: Querying data from MySQL server using the SELECT statement, we learned how to generate the ER diagram using reverse-engineering the database using MySQL workbench and basics of SELECT statement and usage. Ins this article, we will learn how we can sort and filter data using the WHERE clause and sort the data using the ORDER BY clause.

在本文中,我们将学习如何使用WHERE子句对数据进行排序和筛选,以及如何使用ORDER BY子句对数据进行排序。 在我的上一篇文章“ 学习MySQL:使用SELECT语句从MySQL服务器查询数据”中 ,我们学习了如何使用MySQL工作台以及SELECT语句和用法的基础知识对数据库进行反向工程来生成ER图。 在本文中,我们将学习如何使用WHERE子句对数据进行排序和筛选,以及如何使用ORDER BY子句对数据进行排序。

使用WHERE子句过滤数据 (Filtering data using the WHERE clause)

To filter data, we use the WHERE clause in the SELECT query. The syntax is as follows:

为了过滤数据,我们在SELECT查询中使用WHERE子句。 语法如下:

SELECT <COLUMN_LIST>
FROM
TABLE_NAME
WHERE
condition

Here, the condition can be combined with one or more than one logical expression. These logical expressions are called predicates. The predicates or logical expression can be evaluated as TRUE, FALSE, or UNKNOWN. When the WHERE clause does not return any row, then the predicated is evaluated as FALSE. If it returns the row, it will be included in the result-set, and the predicate is evaluated as TRUE.

在此, 条件可以与一个或多个逻辑表达式组合。 这些逻辑表达式称为谓词。 谓词或逻辑表达式可以评估为TRUE,FALSE或UNKNOWN。 当WHERE子句不返回任何行时,则谓词被评估为FALSE。 如果它返回该行,它将被包含在结果集中,并且该谓词被评估为TRUE。

To demonstrate, we are going to use the customer table of the sakila database. In this article, I will demonstrate various use cases of the WHERE clause and plan to cover the following use cases:

为了演示,我们将使用sakila数据库的customer表。 在本文中,我将演示WHERE子句的各种用例,并计划涵盖以下用例:

  1. The WHERE clause with an equal operator 具有相等运算符的WHERE子句
  2. The WHERE clause with AND, OR, and BETWEEN operators 带AND,OR和BETWEEN运算符的WHERE子句
  3. The WHERE clause with LIKE and IN operators 带LIKE和IN运算符的WHERE子句
  4. The WHERE clause with comparison operators 带比较运算符的WHERE子句

具有相等运算符的WHERE子句 (The WHERE clause with an equal operator)

The following query populates the list of customers whose first name is ‘LINDA‘, to retrieve the records, the query should be written as follows:

以下查询将填充名字为“ LINDA ”的客户列表,以检索记录,查询应编写如下:

select * from customer where first_name='LINDA';

The output is below:

输出如下:

In the query, the predicate is WHERE first_name = ‘LINDA ‘, which is evaluated as true.

在查询中,谓词为WHERE first_name ='LINDA',其评估结果为true。

带AND运算符的WHERE子句 (The WHERE clause with AND operator)

In this example, I will show how we can use multiple predicates to populate the desired records from the table. For example, we want to populate the customer whose address_id is 598 and store_id=1. To retrieve the records from the table, the query should be written as follows:

在此示例中,我将展示如何使用多个谓词从表中填充所需的记录。 例如,我们要填充address_id为598和store_id = 1的客户 。 要从表中检索记录,查询应编写如下:

select * from customer where address_id = 598 and store_id=1

The following is the output:

以下是输出:

Here we are including AND operator in the WHERE clause. The expression is WHERE address_id=598 and store_id=1. If both expressions are evaluated as true, then the entire expression is evaluated as TRUE. In the table, we have a record whose address_id=598 and store_id=1; therefore, the entire expression is evaluated as TRUE, and query returned the result-set.

在这里,我们在WHERE子句中包含AND运算符。 表达式是WHERE address_id = 598和store_id = 1 。 如果两个表达式都被评估为真,那么整个表达式将被评估为真。 在表中,我们有一条记录,其address_id = 598和store_id = 1; 因此,整个表达式的求值为TRUE,并且查询返回了结果集。

带OR运算符的WHERE子句 (The WHERE clause with OR operator)

For example, we want to populate the customer whose store_id=1 OR active=0. To retrieve the records, the query should be written as follows:

例如,我们要填充store_id = 1或active = 0的客户 。 要检索记录,查询应编写如下:

select * from customer where store_id=1 OR active=0

The following is the output:

以下是输出:

Here we are including OR operator in the WHERE clause. The expression is WHERE store_id=1 OR active=0. From both expressions, any one of them evaluated as true; then, the entire expression is evaluated as TRUE. In the table, we have a record whose store_id=1 OR active=0; therefore, the entire expression is evaluated as TRUE, and query returned the result-set.

在这里,我们在WHERE子句中包含OR运算符。 表达式为WHERE store_id = 1或active = 0 。 从这两个表达式中,它们中的任何一个都被评估为“真”。 然后,整个表达式的计算结果为TRUE。 在表中,我们有一条记录,其store_id = 1或active = 0; 因此,整个表达式的求值为TRUE ,并且查询返回了结果集。

带BETWEEN运算符的WHERE子句 (The WHERE clause with BETWEEN operator)

For example, we want to populate the customer whose address_id is between 560 and 570. To retrieve the records, the query should be written as follows:

例如,我们要填充address_id在560和570之间的客户。 要检索记录,查询应编写如下:

select * from customer where address_id  between 560 and 570;

The following is the output:

以下是输出:

Here, we included the BETWEEN operator in the WHERE clause. The expression is WHERE address_id is BETWEEN 560 and 570. In the table, we have records where address_id is BETWEEN 560 and 570; therefore, the expression is evaluated as TRUE, and query returned the result-set.

在这里,我们在WHERE子句中包含了BETWEEN运算符。 表达式是WHERE address_id在560和570之间 。 在表中,我们有记录,其中address_id为560和570之间; 因此,该表达式的计算结果为TRUE,并且查询返回了结果集。

IN运算符的WHERE子句 (The WHERE clause with IN operator)

For example, we want to populate the customer whose customer_id in (10,11,12). To retrieve the records, the query should be written as follows:

例如,我们要填充(10,11,12)中的customer_id的客户 。 要检索记录,查询应编写如下:

select * from customer where customer_id in (10,11,12)

The following is the output:

以下是输出:

Here, we included the IN operator in the WHERE clause. The expression is WHERE customer_id in (10,11,12). In the table, we have a record whose customer_id in (10,11,12); therefore, the expression is evaluated as TRUE, and query returned the result-set.

在这里,我们在WHERE子句中包含了IN运算符。 表达式是(10,11,12)中的 WHERE customer_id 。 在表中,我们有一条记录,其customer_id为(10,11,12); 因此,该表达式的计算结果为TRUE,并且查询返回了结果集。

带比较运算符的WHERE子句 (The WHERE clause with a comparison operator)

In the WHERE clause, we can specify the following comparison operators to match the values in the table. The details of the operators are as following:

在WHERE子句中,我们可以指定以下比较运算符以匹配表中的值。 操作员的详细信息如下:

Comparison Operator

Note

Equal to (=)

This operator can be used with any data type

Not Equal to (<> OR !=)

This operator can be used with any data type

Is greater or equal to (>=)

This operator can be used with numeric and date-time data types

Is greater or equal to (<=)

This operator can be used with numeric and date-time data types

Is greater than (>)

This operator can be used with numeric and date-time data types

Is less than (<)

This operator can be used with numeric and date-time data types

比较运算符

注意

等于(=)

该运算符可以与任何数据类型一起使用

不等于(<> OR!=)

该运算符可以与任何数据类型一起使用

大于或等于(> =)

该运算符可用于数字和日期时间数据类型

大于或等于(<=)

该运算符可用于数字和日期时间数据类型

大于(>)

该运算符可用于数字和日期时间数据类型

小于(<)

该运算符可用于数字和日期时间数据类型

For example, we want to populate the records whose address id is greater than 100; then the query should be written as follows:

例如,我们要填充地址ID大于100的记录。 那么查询应编写如下:

select * from customer where address_id>100

Below is the output:

以下是输出:

Suppose, we want to get the list of the inactive customers from the table. The query should be written as follows:

假设我们要从表中获取非活动客户的列表。 该查询应编写如下:

select * from customer where active=0

Below is the output.

以下是输出。

Now, we want to populate the list of the customers who have been created after 12:47 PM on 22-07-2018. The query should be written as follows:

现在,我们要填充在2018年7月22日中午12:47之后创建的客户列表。 该查询应编写如下:

select * from customer where create_date >'2020-07-22 12:47:00'

Below is the output:

以下是输出:

使用ORDER BY子句对数据进行排序 (Sorting data using ORDER BY clause)

When a SQL query returns the output, the values are not sorted. To sort the result of a query, we use the ORDER BY clause. The syntax of the ORDER BY clause is the following:

当SQL查询返回输出时,将不对值进行排序。 为了对查询结果进行排序,我们使用ORDER BY子句。 ORDER BY子句的语法如下:

SELECT <COLUMN_1>,<COLUMN_2>,.. FROM <TABLE_NAME> ORDER BY <COLUMN_1>

When you specify ASC in the ORDER BY clause, the result will be sorted in ascending order, and if you specify DESC, then the result will be sorted in descending order.

在ORDER BY子句中指定ASC时,结果将按升序排序,如果指定DESC,则结果将按降序排序。

SELECT <COLUMN_1>,<COLUMN_2>,.. FROM <TABLE_NAME> ORDER BY <COLUMN_1> DESC

The default sorting order is Ascending, so if we do not specify the sorting order, then the query result will be sorted in ascending order.

默认排序顺序为升序,因此,如果我们不指定排序顺序,则查询结果将按升序排序。

SELECT <COLUMN_1>,<COLUMN_2>,.. FROM <TABLE_NAME> ORDER BY <COLUMN_1> ASC

We can specify one or more than one column in the ORDER BY clause. The columns and their sorting order must be separated by comma (,). We can specify different sorting orders for the individual column. For example, if you want to sort the first column in ascending order and second column in descending order, then the syntax will be Column_1 ASC, column_2 desc. Here first, the column_1 will be sorted in ascending order and then the column_2 will be sorted in descending order. When the second column is sorted, the order of the values of the first column does not change.

我们可以在ORDER BY子句中指定一列或多于一列。 列及其排序顺序必须用逗号(,)分隔 我们可以为各个列指定不同的排序顺序。 例如,如果要对第一列进行升序排序,对第二列进行降序排序,则语法为Column_1 ASC和column_2 desc 。 在这里,首先,column_1将以升序排序,然后,column_2将以降序排序。 对第二列进行排序时,第一列的值的顺序不变。

For example, I want to see the list of customers in ascending order. The query should be written as follows:

例如,我想按升序查看客户列表。 该查询应编写如下:

select *from customer order by first_name asc

The following is the screenshot of the output:

以下是输出的屏幕截图:

Another example, suppose I want to see the first name of the customer in ascending order and the last name in descending order then, the query should be written as follows:

另一个示例,假设我想按升序查看客户的名字,然后按降序查看姓氏,则查询应编写如下:

select *from customer order by first_name asc, last_name desc;

The output is below:

输出如下:

We can use the ORDER BY clause to sort the result set that is generated by the expression. To demonstrate, I am going to use the rental table of the sakila database. Suppose the rental of the film has been increased. We want to check the updated price of the film, and the sorting must be performed on the updated rental. The query should be written as follows:

我们可以使用ORDER BY子句对表达式生成的结果集进行排序。 为了演示,我将使用sakila数据库的租金表。 假设电影的租金已经增加。 我们要检查胶片的更新价格,并且必须对更新后的租金进行分类。 该查询应编写如下:

select payment_id, customer_id, rental_id, amount as 'Old Price', amount+5.5 as 'New Price' from payment a order by amount+5.5 desc;

Below is the screenshot of the output:

下面是输出的屏幕截图:

Similarly, we can sort the output of the result-set generated by the arithmetic function. For example, I want to find the highest number of films available in the store. The query should be written as follows:

同样,我们可以对算术函数生成的结果集的输出进行排序。 例如,我想在商店中找到数量最多的电影。 该查询应编写如下:

select store_id,count(film_id) as 'Total Films' from inventory
group by store_id
order by  count(film_id) desc

Below is the screenshot of the output:

下面是输出的屏幕截图:

摘要 (Summary)

In this article, we have learned to Sort and filter data generated by a query using the WHERE and ORDER BY clause. I have covered various use cases of the WHERE and ORDER BY clause. In the next article, we will learn about the INSERT statement to add data in the MySQL table and its various use cases with examples. Stay tuned..!!

在本文中,我们学习了使用WHERE和ORDER BY子句对查询生成的数据进行排序和筛选。 我已经介绍了WHERE和ORDER BY子句的各种用例。 在下一篇文章中,我们将通过示例学习有关在MySQL表中添加数据的INSERT语句及其各种用例。 敬请关注..!!

目录 (Table of contents)

Learn MySQL: Querying data from MySQL server using the SELECT statement
Learn MySQL: What is pagination
Learn MySQL: Sorting and Filtering data in a table
学习MySQL:使用SELECT语句从MySQL服务器查询数据
学习MySQL:什么是分页
学习MySQL:对表中的数据进行排序和过滤

翻译自: https://www.sqlshack.com/learn-mysql-sorting-and-filtering-data-in-a-table/

mysql 如何对表排序

mysql 如何对表排序_学习MySQL:对表中的数据进行排序和过滤相关推荐

  1. 人名和成绩一起排序_不同工作表中的数据表排序各异,如何用Excel数据透视表汇总?...

    用数据透视表做分析的时候,有时源数据有好几张数据表,且分布在不同的地方.且因为维护源数据的人不同,数据表的行.列标题也未必固定不变. 那么问题来了,如果各个数据表的行标题排序各不相同,如何将它们合并到 ...

  2. mysql分页是物理分页_学习MySQL:什么是分页

    mysql分页是物理分页 In this article, I am going to explain that in MySQL, what is pagination and how we can ...

  3. java对列表数据排序_如何在Java中对列表进行排序

    java对列表数据排序 Sometimes we have to sort a list in Java before processing its elements. In this tutoria ...

  4. layui 自定义排序_浅谈layui中table的sort排序

    table模块是layui框架最核心的组成之一,它用于对表格进行一些列功能和动态化数据操作,本文介绍了layui中table的sort排序,解决了在我们使用sort排序时可能遇到的一些问题. 今天来谈 ...

  5. data layui table 排序_浅谈layui中table的sort排序

    table模块是layui框架最核心的组成之一,它用于对表格进行一些列功能和动态化数据操作,本文介绍了layui中table的sort排序,解决了在我们使用sort排序时可能遇到的一些问题. 今天来谈 ...

  6. mysql新手注意事项_学习mysql的注意事项!

    增删改查 先学增! 1:数字不用加双引号 2;字符串必须加双引号 3: 更新 所有行的单词:update 指点行:update 你想更新 谁的 就是 你想改哪一行! 需要记住的 有几个关键的问题!改 ...

  7. matlab 日期排序_在Matlab中对数据进行排序(Sorting Data in Matlab)

    在Matlab中对数据进行排序(Sorting Data in Matlab) 我试图在Matlab中对以下数据进行排序,但没有得到我需要的预期输出. 这是数据: '1B-3A-5A' '1A-3A- ...

  8. C# 对Excel表格中的数据进行排序

    使用Excel处理数据时我们经常需要用到数据排序功能,这篇文章将介绍如何使用C#对Excel中的数据进行排序.这里介绍三种排序方式: 基于单元格的值进行排序 基于单元格的背景颜色进行排序 基于单元格的 ...

  9. 对数据库中的数据进行排序

    ** 对数据库中的数据进行排序 ** 使用SELECT语句从表中查询数据时,不保证结果集中的行顺序.这意味着SQL Server可以返回具有未指定顺序的结果集. 保证结果集中的行已排序的方法是使用OR ...

最新文章

  1. python快速小教程
  2. [转摘] JSP连接SQL SERVER问题总结
  3. SpringBoot+MyBatisPlus+ElementUI一步一步搭建前后端分离的项目(附代码下载)
  4. solr文档索引最佳实践
  5. Git之HEAD和origin
  6. 十五、详述 IntelliJ IDEA 插件的安装及使用方法
  7. kindeditor 4 指定生成文件的时间日期/动态获取My97的时间
  8. ubuntu环境下lnmp环境搭建(3)之Php
  9. Java多线程学习三十七:volatile 的作用是什么?与 synchronized 有什么异同
  10. 微信小程序数据拼接_微信小程序使用原生WebSokcet实现断线重连及数据拼接
  11. linux clang安装,linux 配置 clang++ SDL 开发环境 (新手向)
  12. air flow空调上是什么意思_中央空调的直流变频和全直流变频是什么意思?有什么区别吗?...
  13. IMDB TOP250电影介绍(下)
  14. 国产数据库-达梦数据库
  15. 软件测试面试如何正确谈论薪资?
  16. python:金额数字转为人民币大写
  17. Team:Syclover Author:L3m0n Email:iamstudy@126.com
  18. Could not transfer artifact XXX 问题处理
  19. 创业公司,老板说等公司做大了,给5%股权,建议你不要轻易相信
  20. rabbitmq链接超时_RabbitMQ前置SLB中TCP连接超时900秒限制

热门文章

  1. linux工具-journalctl查询日志
  2. 区块链如何赋能车联网-Higgs Chain
  3. 数据库设计的三大范式通俗解释
  4. 用JS获取地址栏参数的方法(超级简单)
  5. 使用Bochs调试Linux kernel 随笔 -- 准备
  6. 数据结构---AVL树调整方法(详)
  7. C++---堆代码实现
  8. 【王道考研计算机网络】—OSI参考模型
  9. 【博客项目】—用户修改功能(十一)
  10. 你认识的有钱人,是怎么起家的?是做什么生意的?