从desc根据ID排序的表中选择最上面的记录?
为此,将ORDER BY DESC与LIMIT 1一起使用。让我们首先创建表-
mysql> create table DemoTable-> (
-> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> UserName varchar(100),
-> UserMessage text
-> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(UserName,UserMessage) values('Adam','Hi');mysql> insert into DemoTable(UserName,UserMessage) values('Chris','Awesome');
mysql> insert into DemoTable(UserName,UserMessage) values('Robert','Nice');
mysql> insert into DemoTable(UserName,UserMessage) values('David','Good Morning!!!');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
这将产生以下输出-
+----+----------+-----------------+| Id | UserName | UserMessage |
+----+----------+-----------------+
| 1 | Adam | Hi |
| 2 | Chris | Awesome |
| 3 | Robert | Nice |
| 4 | David | Good Morning!!! |
+----+----------+-----------------+
4 rows in set (0.00 sec)
以下是从表中选择最高记录的查询-
mysql> select *from DemoTable order by Id DESC limit 1;
输出结果
这将产生以下输出-
+----+----------+-----------------+| Id | UserName | UserMessage |
+----+----------+-----------------+
| 4 | David | Good Morning!!! |
+----+----------+-----------------+
1 row in set (0.00 sec)
以上是 从desc根据ID排序的表中选择最上面的记录? 的全部内容, 来源链接: utcz.com/z/316337.html