Get the App
SLTechnology News&Howtos  ›  Development  › 

How to query rankings for php mysql

Shulou Source: shulou.com Published: 2022-06-02 20:06:22 09月29日 Update

This article mainly explains "how to query rankings in php mysql". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "php mysql how to query rankings" bar!

Php mysql query ranking method: 1, rank all users through SQL statement; 2, through "SELECT b.uidjinb.rownum FROM (SELECT t.ranking, @ rownum:...)" Statement to query the ranking of a particular user.

This article operating environment: Windows7 system, PHP7.1 version, DELL G3 computer

How does php mysql query rankings?

PHP+mysql implements ranking and queries the ranking of specified users

The situation is as follows:

Rank the points of all users in the user table [point] and get the specific ranking of a user's points.

1. First, rank all users

$sql = "SELECT t.questions, @ rownum: = @ rownum + 1 AS rownum FROM (SELECT @ rownum: = 0) r, (SELECT uid,point FROM user ORDER BY `point` DESC) AS t"; $sql = "SELECT t.questions, @ rownum: = @ rownum + 1 AS rownum FROM (SELECT @ rownum: = 0) r, (SELECT uid,point FROM user ORDER BY `point` DESC) AS t"

Note: 1. SELECT @ rownum: = 0: an initial value of 0 is assigned to rownum.

2. @ rownum: = @ rownum + 1: for rownum plus 1, the statement starts with 1, and each line is automatically added with 1.

The above code sorts from largest to smallest according to the point in the user table.

2. Get the ranking of a specific user

Principle: treat the data obtained in the previous step as a table, and query the ranking of specific users according to uid

$sql = "SELECT b.uidjie b.rownum FROM (SELECT t.rownum, @ rownum: = @ rownum + 1 AS rownum FROM (SELECT @ rownum: = 0) r, (SELECT uid,point FROM user ORDER BY `point` DESC) AS t) AS b WHERE b.uid = {$uid}"

Query result: {"uid": "300462", "rownum": "10"}

Where rownum is the ranking of the user.

At this point, I believe that everyone on the "php mysql how to query rankings" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Query user statement content method integral learning practical deeper one line code interest principle size practicality reality situation simple operation data more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker Huawei NVidia MariaDB