php-在Mysql语句中包括GROUP BY id

我已经实现了一种用于过滤表的解决方案,但是这也使我失去了想要的主要功能之一. GROUP BY ID DESC,因此最新信息会发布在顶部.

第一次查看没有任何过滤器的表时,我希望它也是GROUP BY ID DESC,但是设置过滤器的方式我无法做到.有什么解决方法吗?我的代码如下

<?php 
// Check connection
require '../inc/connection.php';
//Select All from Game
$result_else = "SELECT * FROM game WHERE listDate >= NOW() - INTERVAL 1440 MINUTE ";

//Search Function
if (isset($_POST['search'])) {
$search_term = mysqli_real_escape_string($con, $_POST['search_box']);   
$result_else .= "AND Region LIKE'%{$search_term}%'";
$result_else .= "OR gameType LIKE'%{$search_term}%' GROUP BY id DESC LIMIT 25";
}

?>

如果我希望没有任何过滤器的表的原始视图具有GROUP BY ID DESC LIMIT 25,将如何进行?

谢谢你的帮助

解决方法:

是的,可以做到

$result_else = "SELECT * FROM game WHERE listDate >= NOW() - INTERVAL 1440 MINUTE ";

//Search Function
if (isset($_POST['search'])) {
    $search_term = mysqli_real_escape_string($con, $_POST['search_box']);   
    $result_else .= "AND Region LIKE'%{$search_term}%'";
    $result_else .= "OR gameType LIKE'%{$search_term}%' ";
}
// $result_else .= "GROUP BY id DESC LIMIT 25";
$result_else .= "ORDER BY id DESC LIMIT 25";

但不确定您要的是GROUP BY还是ORDER BY

GROUP BY将内容分组.

ORDER BY以asc或desc顺序订购内容

上一篇:php-无法连接到Wamp中的数据库


下一篇:PHP-mysqli :: $field_count与mysqli_result :: $field_count