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_nameLIMIT number
We will consider the following table for understanding SQL Limit in a better way.
我们将考虑下表,以更好地理解SQL Limit。
Student
学生
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:
输出:
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 withWHEREclause.
SQL Limit也可以与WHERE子句一起使用。
SQL Limit with Where Clause Syntax:
带Where子句语法SQL限制:
SELECT column_name(s)FROM table_name WHERE conditionLIMIT 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:
输出:
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_nameLIMIT 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子句可帮助我们在应用程序中实现分页。 如果我们必须通过限制结果集的大小来处理庞大的结果集数据,这将非常有帮助。
翻译自: /24379/sql-limit-mysql-limit
mysql sql长度限制