dtcyv3985 2014-01-07 05:21
浏览 120
已采纳

从mysql表中提取唯一值,并按出现次数排序

I'm having a little trouble with this, I found how to get unique values from multiple columns, and I know how to sort normally, but I can't seem to mix the two

The original sql statement is here:

$sql="SELECT DISTINCT UserID, UserIP FROM Visits WHERE PageID='".$_GET['p']."'";

It only gets the first instance of where both values are unique, so doing the 'mysql_numrows' thing doesn't work.

This is from within the fetch array loop, it can count the occurrences fine, but at that point it's a bit too late to sort

$sql5="SELECT Date, VisitID FROM Visits WHERE PageID='".$_GET['p']."' AND UserID='".$userid."' AND UserIP='".$userip."' ORDER BY Date";
$result5 = mysql_query($sql5);
$num5 = mysql_numrows($result5);

Basically, it should get unique values based on both columns, count how many times the appear, sort by count, then potentially also sort by the highest matching date if possible

Any suggestions would be great, cheers :)
Also it's just for a personal site, I'm not actually doing anything with the info, just trying to learn some more php

  • 写回答

2条回答 默认 最新

  • douji8017 2014-01-07 05:25
    关注

    Try using GROUP BY, like this:

    SELECT UserID, UserIP FROM Visits 
    WHERE PageID = ###
    GROUP BY UserID, UserIP
    ORDER BY COUNT(*) DESC, max(`Date`) DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思