duan2891 2015-05-23 10:54
浏览 37
已采纳

MYSQL:GROUP BY问题使用ORDER BY

here is query with pagination

$page = (int)(!isset($_GET["page"]) ? 1 : $_GET["page"]);

    if ($page <= 0) $page = 1;

    $per_page = 2; // Set how many records do you want to display per page.

    $startpoint = ($page * $per_page) - $per_page;

    $statement = "`message_box` WHERE reciver = '".dec($_SESSION['user_reporter'])."' AND status='0' GROUP BY `sender` ORDER BY `date_added` DESC";

    $result = mysql_query("SELECT count(*),id,sender,message,status,date_added FROM {$statement} LIMIT {$startpoint} , {$per_page}");

and at the end of the table where my data is displayed, i call my pagination function ( define in my custom-function.php ).

So, with the above query my pagination block is not displaying. But When I remove

GROUP BY `sender`

FROM $statement like this,

$statement = "`message_box` WHERE reciver = '".dec($_SESSION['user_reporter'])."' AND status='0' GROUP BY `sender` ORDER BY `date_added` DESC";

pagination displays and works fine but i want that GROUP BY sender clause.

sorry for my bad English....please...help !

  • 写回答

1条回答 默认 最新

  • duanqian2368 2015-05-23 11:31
    关注

    This query:

    SELECT count(*), id, sender, message, status, date_added
    FROM {$statement}
    LIMIT {$startpoint}, {$per_page}
    

    is going to return one row, regardless of the LIMIT clause. It is an aggregation query without a GROUP BY clause. Such a query is an aggregation over the whole table and by definition returns one row.

    Remove the count(*) if you want all the rows. Add GROUP BY id, sender, message, status, date_added if you want to count the number of duplicates you have for those columns.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题