简体   繁体   中英

MYSQL limit the group by records fetch to 3

I want to limit the array to 3 which is been fetch used Group by.

The query is as below:

$sql_time = "SELECT date FROM tb_player_activity WHERE user_id='$data[1]' GROUP BY date DESC"; 

I want to display only 3 records of the array which is been fetched

使用mysql限制

$sql_time = "SELECT date FROM tb_player_activity WHERE user_id='$data[1]' GROUP BY date DESC limit 3";

Use LIMIT

$sql_time = "SELECT date FROM tb_player_activity 
              WHERE user_id='$data[1]' 
              GROUP BY date DESC 
              LIMIT 3"; 

MySQL LIMIT Clause. In MySQL the LIMIT clause is used with the SELECT statement to restrict the number of rows in the result set. The Limit Clause accepts one or two arguments which are offset and count.The value of both the parameters can be zero or positive integers.

https://www.geeksforgeeks.org/php-mysql-limit-clause/

SELECT date FROM tb_player_activity WHERE user_id='$data[1]' GROUP BY date DESC limit 0,3

注意 :像这样传递参数将导致SQL注入

尝试限制:

$sql_time = "SELECT date FROM tb_player_activity WHERE user_id='$data[1]' ORDER BY date DESC LIMIT 3";

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