简体   繁体   English

php,mysql-使用mysql在id列中选择最后一个内容

[英]php,mysql-select last content in id column using mysql

嘿,在一个Pokeheroes站点中,有一个“ 注册用户: 54,805 ”。 ID中的数字是,然后显示它。请帮助我

@viam @viam

If you use count(*) , it returns only the number of records in the table. 如果使用count(*) ,则它仅返回表中的记录数。 so it is better to use: 所以最好使用:

SELECT MAX(id) AS max_userid
FROM USERS;

You got it already by using MAX() aggregate function but there is another way by using ORDER BY clause like below; 您已经通过使用MAX()聚合函数获得了它,但是通过使用ORDER BY子句还有另一种方法,如下所示; which will order the ID column in descending order and so the maximum ID number will be at top and from there you can use LIMIT clause to get the top result. 它将以降序对ID列进行排序,因此最大ID数将位于顶部,然后您可以使用LIMIT子句获得顶部结果。

SELECT ID AS MAX_ID
FROM USERS
ORDER BY ID DESC
LIMIT 1;

There are two possibilities to fetch total number of users present in you database. 有两种方法可以获取数据库中存在的用户总数。 1. get the last inserted id 2. get total number of rows in the table(there may be possibilities some users list may get deleted from table) 1.获取最后插入的ID 2.获取表中的总行数(某些用户列表可能会从表中删除)

Bernd Buffen answer is smart . Bernd Buffen的答案很明智。 you can also use another query if you want & this is- 您还可以根据需要使用其他查询,这是-

SELECT id AS max_userid
FROM table_name
ORDER BY id DESC 
LIMIT 1

and for point no 2 I will suggest viam's answer is good because this will give how ACTUAL users are present in your db instead of showing last inserted id you can show actual users. 对于第2点,我建议viam的回答是好的,因为这将提供ACTUAL用户在数据库中的显示方式,而不是显示最后插入的ID,而可以显示实际用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM