mysql sql长度限制

When we work with a huge amount of data there are cases when we need to restrict the number of rows that should be returned as part of the query.

当我们处理大量数据时,有时需要限制应作为查询的一部分返回的行数。

In order to help us with the same, SQL provides us with a feature called SQL Limit. This clause has to be the last one in the query otherwise you will get an error.

为了帮助我们做到这一点,SQL为我们提供了一个称为SQL Limit的功能。 此子句必须是查询中的最后一个子句,否则会出现错误。

SQL限制子句 (SQL Limit Clause)

MySQL, PostgreSQL and many more databases support the usage of SQL Limit clause. The SQL query is executed and finally, the number of results specified by the LIMIT clause is returned. If the result set size is smaller than the rows specified by the LIMIT clause, then all the result set is returned.

MySQL,PostgreSQL和许多其他数据库支持使用SQL Limit子句。 执行SQL查询,最后返回由LIMIT子句指定的结果数。 如果结果集大小小于LIMIT子句指定的行,则返回所有结果集。

Let’s try to understand the usage of SQL Limit clause.

让我们尝试了解SQL Limit子句的用法。

SQL限制示例 (SQL Limit Example)

Syntax:

语法

SELECT column_name(s)
FROM table_name
LIMIT number

We will consider the following table for understanding SQL Limit in a better way.

我们将考虑下表,以更好地理解SQL Limit。

Student

学生

StudentId StudentName State Country
1 Henry Wales UK
2 Rohit Delhi India
3 Steve London UK
学生卡 学生姓名 国家
1个 亨利 威尔士 英国
2 罗希特 新德里 印度
3 史蒂夫 伦敦 英国

Below is the query to create the table and insert some sample data. Note that I am using PostgreSQL, so you might have to make some changes to run it in other databases.

下面是创建表并插入一些示例数据的查询。 请注意,我使用的是PostgreSQL,因此您可能必须进行一些更改才能在其他数据库中运行它。

CREATE TABLE `student` (`StudentId` INT NOT NULL,`StudentName` VARCHAR(45) NULL,`State` VARCHAR(45) NULL,`Country` VARCHAR(45) NULL,PRIMARY KEY (`StudentId`),UNIQUE INDEX `StudentId_UNIQUE` (`StudentId` ASC) VISIBLE);Insert into Student(StudentId,StudentName,State,Country) VALUES (1, 'Henry','Wales','UK'), (2, 'Rohit','Delhi','India'), (3, 'Steve','London','UK');

Now we will try to limit the display result to just 2 rows.

现在,我们将尝试将显示结果限制为仅两行。

SELECT * FROM student limit 2;

Output:

输出:

StudentId StudentName State Country
1 Henry Wales UK
2 Rohit Delhi India
学生卡 学生姓名 国家
1个 亨利 威尔士 英国
2 罗希特 新德里 印度

SQL Limit

SQL限制

There are times when we need to limit the number of rows based on some condition. We will try to understand the scenario better in the next section.

有时我们需要根据某些条件限制行数。 在下一节中,我们将尝试更好地理解这种情况。

带where子句SQL限制 (SQL Limit with Where Clause)

SQL Limit can also be used along with WHERE clause.

SQL Limit也可以与WHERE子句一起使用。

SQL Limit with Where Clause Syntax:

带Where子句语法SQL限制:

SELECT column_name(s)
FROM table_name WHERE condition
LIMIT number

We will consider the same student table for this case as well.

在这种情况下,我们还将考虑相同的学生表。

Let us try to get one entry from the table with the country as UK.

让我们尝试从表格中获得一个国家/地区为英国的条目。

SELECT * FROM student where country= "UK" limit 1;

Output:

输出:

StudentId StudentName State Country
1 Henry Wales UK
学生卡 学生姓名 国家
1个 亨利 威尔士 英国

SQL Limit with Where clause

带限制子句SQL限制

MySQL限制 (MySQL Limit)

MySQL database also supports LIMIT clause. Above examples are from PostgreSQL database. Let’s look at some examples from MySQL database.

MySQL数据库还支持LIMIT子句。 上面的示例来自PostgreSQL数据库。 让我们来看一些来自MySQL数据库的示例。

Here is the script to create a sample table with some data.

这是用于创建包含一些数据的样本表的脚本。

CREATE TABLE `Customer` (`CustomerId` int(11) unsigned NOT NULL AUTO_INCREMENT,`CustomerName` varchar(20) DEFAULT NULL,`CutomerAge` int(11) DEFAULT NULL,`CustomerGender` varchar(2) DEFAULT NULL,PRIMARY KEY (`CustomerId`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;INSERT INTO `Customer` (`CustomerId`, `CustomerName`, `CutomerAge`, `CustomerGender`)
VALUES(1, 'John', 31, 'M'),(2, 'Amit', 25, 'M'),(3, 'Annie', 35, 'F'),(4, 'Tom', 38, 'M');

Let’s run a simple query to select only 3 rows from the Customer table.

让我们运行一个简单的查询,从“客户”表中仅选择3行。

select * from Customer limit 3;

We can use Order By clause with LIMIT too. In this case, the query will be executed and finally, only the number of rows specified by the limit clause will be returned.

我们也可以在LIMIT中使用Order By子句。 在这种情况下,将执行查询,最后仅返回由limit子句指定的行数。

select * from Customer order by CustomerName limit 2;

Finally, let’s look at an example of using the LIMIT clause with WHERE condition.

最后,让我们看一个在WHERE条件下使用LIMIT子句的示例。

select * from Customer where CustomerGender = 'M' limit 2;

MySQL限制偏移 (MySQL Limit Offset)

Most of the times, the limit clause is used to process bulk records with pagination. So it’s useful when it’s used with offset. The syntax of the limit clause with offset is:

在大多数情况下,limit子句用于通过分页处理批量记录。 因此,在与offset一起使用时很有用。 带偏移量的limit子句的语法为:

SELECT column_name(s)
FROM table_name
LIMIT offset_number, number

The offset number specifies the rows to skip before the specified number of rows to be returned in the result. Let’s understand this with a simple example.

偏移量编号指定要在结果中返回指定行数之前跳过的行。 让我们用一个简单的例子来理解这一点。

Select * from Customer limit 1, 2;

Our Customer table has 4 rows. So the first row is skipped and the next two rows are returned in the result. Let’s look at one more example for SQL limit offset query.

我们的客户表有4行。 因此,将跳过第一行,并在结果中返回后两行。 让我们再看一个SQL限制偏移量查询的示例。

select * from Customer limit 2, 1;

Here only the third row from the Customer table will be returned.

在这里,仅返回客户表的第三行。

结论 (Conclusion)

SQL LIMIT clause helps us in achieving pagination in our application. It’s very helpful if we have to process huge result-set data by limiting the result set size.

SQL LIMIT子句可帮助我们在应用程序中实现分页。 如果我们必须通过限制结果集的大小来处理庞大的结果集数据,这将非常有帮助。

翻译自: https://www.journaldev.com/24379/sql-limit-mysql-limit

mysql sql长度限制

mysql sql长度限制_SQL限制– MySQL限制相关推荐

  1. mysql sql高级教程_SQL高级教程

    一.top子句 top子句用于规定要返回的记录的数目 并非所有数据库系统都支持top子句 # sqlserverSELECT TOP number|percent column_name(s) FRO ...

  2. mysql与sql哪个简单_Sql与MySQL简单入门

    作为过来人,给"新司机"一点建议:运维时需要搭建的生产环境,需尽量保持与测试环境一致:但搭建环境时,又苦于找不到合适的版本怎么办?不用怕,我是一个体贴的人,管杀也管埋(该链接为My ...

  3. mysql sql使用序列_SQL 使用序列

    SQL 使用序列 序列是根据需要产生的一组有序整数:1, 2, 3 ... 序列在数据库中经常用到,因为许多应用要求数据表中的的每一行都有一个唯一的值,序列为此提供了一种简单的方法. 本节阐述在 My ...

  4. mysql sql语句联系_sql语句练习50题(Mysql版)

    表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id ...

  5. mysql sql优化_浅谈mysql中sql优化

    说到sql优化,一般有几个步骤呢,在网上看到了一篇很不错的帖子.在这分享一下吧,也是自己学习的一个过程. 一.查找慢查询 1.1.查看SQL执行频率 SHOW STATUS LIKE 'Com_%'; ...

  6. mysql smallint 长度_转:mysql中int、bigint、smallint 和 tinyint的区别与长度的含义

    bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 个字节. P.S. b ...

  7. mysql sql监控_如何对mysql语句进行监控

    快速阅读 为什么要监控sql语句,以及如何监控,都有哪几种方式可以监控. 我们知道sql server 中有个工具叫sql profile ,可以实时监控sql server中 执行的sql 语句,以 ...

  8. mysql sql注入工具_SQL注入工具实践

    程序简介 超级SQL注入工具(SSQLInjection)是一款基于HTTP协议自组包的SQL注入工具,支持出现在HTTP协议任意位置的SQL注入,支持各种类型的SQL注入,支持HTTPS模式注入. ...

  9. mysql sql注入例子_SQL注入的实际案例

    发动SQL注入要做的三件事 确定Web应用程序所使用的技术 为了确定所采用的技术,攻击者可以考察Web页面的页脚,查看错误页面,检查页面源代码,或者使用诸如Nessus.AWVS.APPSCAN等工具 ...

最新文章

  1. 2018年中美自动驾驶进展分析报告
  2. TF之DNN:TF利用简单7个神经元的三层全连接神经网络【2-3-2】实现降低损失到0.000以下
  3. 功能性农业未来方向-农业大健康·徐春晖:农业品牌市场规范
  4. Linux的phpize
  5. android 启动页_App启动优化一顿操作猛如虎
  6. 京东抽奖项目开发笔记
  7. Python零基础学习笔记(二十)—— tuple元组
  8. 敏捷开发用户故事系列之六:用户故事的产生与组织结构
  9. new blob文件设置编码_前端下载文件amp;下载进度
  10. FMEA软件框图及接口功能库(FMEAHunter)
  11. wps或者word点击打印预览时出现空白页,不显示内容
  12. 支气管炎的饮食要注意哪些
  13. excel数据分组存到一个excel的多个sheet中
  14. 汽车Bootloader流程
  15. 计算机桌面整洁,想让你的桌面变得整洁干净,这几款桌面整理软件别错过
  16. Tangent Convolutions 切面卷积(切线卷积)
  17. 第三方移动支付类产品竞品分析:支付宝VS微信支付VS云闪付
  18. FZU-2268 Cutting Game
  19. 2020有道翻译 使用付费api
  20. python蓝桥杯准备

热门文章

  1. linux内核代码container_of
  2. java中的异常和处理
  3. 在无法单步调试的情况下找Bug的技巧
  4. [LeetCode] Merge Sorted Array
  5. C#的解题思路(1):不重复随机数的产生问题
  6. 关于div布局中float的使用
  7. [转载] 递归函数python基例_python递归函数详解 python 递归函数使用装饰器
  8. [转载] python win32api 使用小技巧
  9. verilog读入.txt的有符号十进制数,把有符号十进制数写入到.txt文件中
  10. 字符和字符串在Java中的旅程