SQL LIMIT函数

SQL LIMIT函数

结构化查询语言(SQL)中的LIMIT函数根据指定的限制值返回表中的记录。

并非所有的SQL版本都支持LIMIT函数。需要注意的是,LIMIT的值必须是非负整数。

LIMIT函数的语法

在SQL中,我们可以将LIMIT函数与字符串和整数类型的列一起使用。

SELECT Column_Name1, Column_Name2, Column_Name3, ……., Column_NameN FROM Table_Name LIMIT Value;

在这个语法中,我们必须在表名之后使用LIMIT关键字及其值进行指定。

LIMIT函数的示例

示例1: 这个示例使用LIMIT函数与结构化查询语言中的表。

以下代码展示了如何在结构化查询语言中创建新表:

CREATE TABLE Name_of_New_Table
(
First_Column_of_table Data Type (character_size of First Column),  
Second_Column_of_table Data Type (character_size of the Second column ),  
Third_Column_of_table Data Type (character_size of the Third column),  
??.,  
Last_Column_of_table Data Type (character_size of the Last column)
);  

下面的CREATE语句创建了 Fresher_Marks 表:

CREATE TABLE Fresher_Marks
(
Fresher_ID INT PRIMARY KEY,  
First_Name VARCHAR (100),  
Middle_Name Varchar(120),
Last_Name VARCHAR (200), 
City Varchar(120),
Aptitude_Marks INT, 
Reasoning_Marks INT, 
Technical_Marks INT,
Percentage INT
);

以下的INSERT查询将记录新生的成绩和详细信息插入到 Fresher_Marks 表中:

INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (501, Vinay, Roy, Gupta, Lucknow, 85, 92, 78, 85 );

INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (502, Monu, Roy, Singhania, Chandigarh, 54, 68, 98, 88 );

INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (504, Ravi, Roy, Kumar, Lucknow, 71, 82, 69, 71 );

INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (507, Shyam, Roy, Sharma, Delhi, 85, 90, 68, 78 );

INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (510, Abhay, Kumar, Gupta, Chandigarh, 45, 68, 82, 78);

INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (509, Riya, Roy, Sharma, Delhi, 68, 90, 69, 91 );


INSERT INTO Fresher_Marks (Fresher_ID, First_Name, Middle_Name, Last_Name, City, Aptitude_Marks, Reasoning_Marks, Technical_Marks, Percentage) VALUES (505, Vishal, Kumar, Sharma, Mumbai, 75, 65, 88, 75 );

下面的SELECT语句显示了上述 Fresher_Marks 表中插入的记录:

SELECT * FROM Fresher_Marks; 
Fresher_ID First_Name Middle_Name Last_Name City Aptitude_Marks Reasoning_Marks Technical_Marks Percentage
501 Vinay Roy Gupta Lucknow 85 92 78 85
502 Monu Roy Singhania Chandigarh 54 68 98 88
504 Ravi Roy Kumar Lucknow 71 82 69 71
507 Shyam Roy Sharma Delhi 85 90 68 78
510 Abhay Kumar Gupta Chandigarh 45 68 82 78
509 Riya Roy Sharma Delhi 68 90 69 91
505 Vishal Kumar Sharma Mumbai 75 65 88 75

查询 1: 以下 SELECT 查询使用 LIMIT 关键字显示上述表的前五行:

SELECT * FROM Fresher_Marks LIMIT 5;

输出:

Fresher_ID First_Name Middle_Name Last_Name City Aptitude_Marks Reasoning_Marks Technical_Marks Percentage
501 Vinay Roy Gupta Lucknow 85 92 78 85
502 Monu Roy Singhania Chandigarh 54 68 98 88
504 Ravi Roy Kumar Lucknow 71 82 69 71
507 Shyam Roy Sharma Delhi 85 90 68 78
510 Abhay Kumar Gupta Chandigarh 45 68 82 78

查询 2: 以下 SELECT 查询在 ORDER BY 子句中使用 LIMIT 关键字与 DESC 查询,以显示最后五个百分比:

SELECT * FROM Fresher_Marks ORDER BY Percentage DESC LIMIT 5;

输出:

Fresher_ID First_Name Middle_Name Last_Name City Aptitude_Marks Reasoning_Marks Technical_Marks Percentage
509 Riya Roy Sharma Delhi 68 90 69 91
502 Monu Roy Singhania Chandigarh 54 68 98 88
501 Vinay Roy Gupta Lucknow 85 92 78 85
507 Shyam Roy Sharma Delhi 85 90 68 78
510 Abhay Kumar Gupta Chandigarh 45 68 82 78

OFFSET关键字与LIMIT函数一起使用

OFFSET关键字与LIMIT函数一起使用,用于指定要显示数据的行。OFFSET的值不能为负数,否则会返回错误。

我们可以将OFFSET的值指定为零或大于零。

LIMIT函数使用OFFSET的示例

以下CREATE语句用于创建 Product_Details 表,用于存储产品的价格和数量:

CREATE TABLE Product_Details
(
Product_ID INT NOT NULL,
Product_Name Varchar(50),
Product_Quantity INT,
Purchasing_Price INT,
Selling_Price INT,
Release_Date Date, 
Product_Rating INT
);

以下是多个INSERT查询,将带有销售和购买价格的产品记录插入到Product_Details表中:

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.1, P1, 0.250, 0.5, NULL, 2022-04-30, NULL);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.2, P4, 5.500, 0.45, -0.8, 2022-01-28, 0.25);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.3, P2, 0.350, 0.85, 0.250, 2022-02-18, 0.15);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.4, P7, 0.850, 0.355, NULL, 2021-12-25, 0.45);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.5, P6, 0.900, 0.5, -0.500, 2021-10-15, NULL);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.6, P8, 0.750, 0.110, -0.95, 2022-01-28, -0.9);

INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.7, P10, 0.250, 0.550, 0.258, 2022-04-11, NULL);

下面的SELECT语句显示了上述 Product_Details 表中插入的记录:

SELECT * FROM Product_Details; 
Product_ID Product_Name Product_Quantity Purchasing_Price Selling_Price Release_Date Product_Rating
0.1 P1 0.250 0.5 NULL 2022-04-30 NULL
0.2 P4 0.500 0.45 -0.8 2022-01-28 0.25
0.3 P2 0.350 0.85 0.250 2022-02-18 0.15
0.4 P7 0.850 0.355 NULL 2021-12-25 0.45
0.5 P6 0.900 0.5 -0.500 2021-10-15 NULL
0.6 P8 0.750 0.110 -0.95 2022-01-28 -0.9
0.7 P10 0.250 0.550 0.258 2022-04-11 NULL

查询1: 以下SELECT查询使用了LIMIT函数和OFFSET关键字:

SELECT * FROM Product_Details ORDER BY Product_ID DESC LIMIT 3 OFFSET 3;
Product_ID Product_Name Product_Quantity Purchasing_Price Selling_Price Release_Date Product_Rating
0.4 P7 0.850 0.355 NULL 2021-12-25 0.45
0.3 P2 0.350 0.85 0.250 2022-02-18 0.15
0.2 P4 0.500 0.45 -0.8 2022-01-28 0.25

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程