简体   繁体   中英

php,mysql-select last content in id column using mysql

嘿,在一个Pokeheroes站点中,有一个“ ”。 ID中的数字是,然后显示它。请帮助我

@viam

If you use count(*) , it returns only the number of records in the table. 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; 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.

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)

Bernd Buffen answer is smart . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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