Home>Article>Backend Development> PHP implements ranking and queries the ranking of specified users
How to implement PHP query ranking: First assume a user table. At this time, users need to be ranked according to the number of customers; then we need to rank the users in the user table, and the statement is "$sql = "SELECT p.name,p.number"; finally output the query results.
PHP implements ranking and queries the ranking of specified users
As shown in the figure:Assuming a user table, users need to be ranked according to the number of number customers.
So, we need to rank the users in the user table:
$sql = "SELECT p.name,p.number, @rownum := @rownum + 1 AS rownum FROM (SELECT @rownum := 0) r, (SELECT * FROM ruser ORDER BY number DESC) AS p"*Note: *1. SELECT @rownum := 0: means assigning an initial value of 0 to rownum
2. @rownum := @rownum 1: means adding 1 to rownum, the statement will Starting from 1, each row will automatically add 1
. The query results are as follows:
As shown in the figure, the above code will increase the number from the largest to the highest according to the number in the user table. Sort by smallest.
If you need to query the ranking of user Xiao Wang, you must query the ranking based on his openid:
$sql = "SELECT b.openid,b.name,b.number,b.rownum FROM(SELECT t.*, @rownum := @rownum + 1 AS rownum FROM (SELECT @rownum := 0) r,(SELECT * FROM partneruser ORDER BY `number` DESC) AS t) AS b WHERE b.openid = "o4mxs5Tia6Ieayvxiebx8rTc1zO4" ";
The query results are as follows:
The above is the detailed content of PHP implements ranking and queries the ranking of specified users. For more information, please follow other related articles on the PHP Chinese website!