Home > Backend Development > PHP Problem > PHP implements ranking and queries the ranking of specified users

PHP implements ranking and queries the ranking of specified users

卡哇伊
Release: 2023-03-02 08:06:01
Original
3960 people have browsed it

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

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.

PHP implements ranking and queries the ranking of specified users

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"
Copy after login
*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:
PHP implements ranking and queries the ranking of specified usersAs 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" ";
Copy after login

The query results are as follows:
PHP implements ranking and queries the ranking of specified users

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template