doujia9833 2013-05-13 05:51
浏览 69
已采纳

Paginate问题与group by的查询中的count

Ok I usually use a pagination code that counts the total pages, like this

$query = "SELECT COUNT(*) as num FROM $tableName WHERE `ganador2` = '1'";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];

then i call the data with this query for echo page contents.

// Get page data
    $query1 = "SELECT id,name,lastname,email,codigo, media, phone, Pcode, birth FROM $tableName  WHERE `ganador2` = '1' LIMIT $start, $limit ";

this last part is the Working code, ok,

Know im trying to use it with this querys, this count how many times the user has entered his email.

$query = "SELECT COUNT(*) AS num,id,name,lastname,email,codigo, media, phone, Pcode, birth FROM usuarios GROUP BY email";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];

and

// Get page data
    $query1 = "SELECT COUNT(*) AS top,id,name,lastname,email,codigo, media, phone, Pcode, birth FROM usuarios GROUP BY email ORDER BY top DESC LIMIT $start, $limit";

The problem is for pagination, its telling me the query just found (2 users) but thats not true, so my query its wrong.. How can i calculate the total pages in the first query, as the second query is working just fine.

thanks

  • 写回答

4条回答 默认 最新

  • duanjiancong4860 2013-05-13 05:57
    关注

    with the group by you are getting a count per email. you are getting the number of records that have the first email... I think this is what you want.

    SELECT COUNT(DISTINCT email) AS num FROM usuarios;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。