sql查询表中ID最大的一条数据肿么写?

发布于2022-01-13 20:18:17
4个回答
admin
网友回答2022-01-13
select top 1 * from table order by id desc;
select * from table order by desc limit 1;
admin
网友回答2022-01-13
创建一个function getChildLst, 得到一个由所有子节点号组成的字符串.

mysql delimiter //
mysql
mysql CREATE FUNCTION `getChildLst`(rootId INT)
- RETURNS varchar(1000)
- BEGIN
- DECLARE sTemp VARCHAR(1000);
- DECLARE sTempChd VARCHAR(1000);
-
- SET sTemp = '$';
- SET sTempChd =cast(rootId as CHAR);
-
- WHILE sTempChd is not null DO
- SET sTemp = concat(sTemp,',',sTempChd);
- SELECT group_concat(id) INTO sTempChd FROM treeNodes where FIND_IN_SET(pid,sTempChd)0;
- END WHILE;
- RETURN sTemp;
- END
- //
Query OK, 0 rows affected (0.00 sec)

mysql
mysql delimiter ;

使用我们直接利用find_in_set函数配合这个getChildlst来查找

mysql select getChildLst(1);
+-----------------+
| getChildLst(1) |
+-----------------+
| $,1,2,3,4,5,6,7 |
+-----------------+
1 row in set (0.00 sec)
admin
网友回答2022-01-13
SELECT * FROM 表名
ORDER BY id DESC
LIMIT 0,1
这样效率更高点
admin
网友回答2022-01-13
select * from table where id=(select MAX(id) from table )

回到
顶部