在MySQL中至少获得x行数?
要获得至少x个行,您需要使用LIMIT子句。以下是语法-
select *from yourTableName order by yourColumnName DESC limit yourXNumberOfRows;
让我们首先创建一个表-
mysql> create table DemoTable ( EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,EmployeeName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(EmployeeName) values('Chris'); mysql> insert into DemoTable(EmployeeName) values('David'); mysql> insert into DemoTable(EmployeeName) values('Bob'); mysql> insert into DemoTable(EmployeeName) values('Robert'); mysql> insert into DemoTable(EmployeeName) values('Mike'); mysql> insert into DemoTable(EmployeeName) values('Sam');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 1 | Chris | | 2 | David | | 3 | Bob | | 4 | Robert | | 5 | Mike | | 6 | Sam | +------------+--------------+ 6 rows in set (0.00 sec)
以下是在MySQL中获取至少x个行的查询。在这里,我们只显示2行-
mysql> select *from DemoTable order by EmployeeName DESC limit 2;
这将产生以下输出-
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 6 | Sam | | 4 | Robert | +------------+--------------+ 2 rows in set (0.02 sec)