Php 使用order by的意外结果

Php 使用order by的意外结果,php,mysql,Php,Mysql,我正在制作一个通知脚本,目前它可以显示关于同一个图像的多个通知,例如john喜欢图像名称,sarah喜欢图像名称->2个人喜欢图像名称 这就是我目前所拥有的 $query = mysql_query("SELECT type, extra_id, COUNT(*), id AS count FROM `notifications` WHERE `receiver_id` = '14' AND `read` = '0' GROUP BY type, extra_id"); while($row =

我正在制作一个通知脚本,目前它可以显示关于同一个图像的多个通知,例如john喜欢图像名称,sarah喜欢图像名称->2个人喜欢图像名称

这就是我目前所拥有的

$query = mysql_query("SELECT type, extra_id, COUNT(*), id AS count FROM `notifications` WHERE `receiver_id` = '14' AND `read` = '0' GROUP BY type, extra_id");
while($row = mysql_fetch_array($query)){

    if($row['count'] == 1){

        switch ($row['type']) {
            case "Following":
                echo "John Doe is now following you <br />";
            break;
            case "Liked":
                $image_id = $row['extra_id'];
                $image_q = mysql_query("SELECT * FROM `images` WHERE `id` = '$image_id' LIMIT 1");
                $image = mysql_fetch_array($image_q);
                echo "John Doe likes ".$image['heading']."<br />";
            break;
        }

    }   else   {

        switch ($row['type']) {
            case "Following":
                echo $row['count']." New Users are following you <br />";
            break;
            case "Liked":
                $image_id = $row['extra_id'];
                $image_q = mysql_query("SELECT * FROM `images` WHERE `id` = '$image_id' LIMIT 1");
                $image = mysql_fetch_array($image_q);
                echo $row['count']." Users Like ".$image['heading']."<br />";
            break;
        }

    }

}
它应该在哪里

2 New Users are following you
John Doe likes IMAGE_ID_29
2 Users like IMAGE_ID_50

您当前的查询是:

SELECT type, extra_id, COUNT(*), id AS count FROM `notifications` WHERE `receiver_id` = '14' AND `read` = '0' GROUP BY type, extra_id
也就是说,您正在使用
id
字段作为
计数

您可能希望将其改写为:

SELECT type, extra_id, COUNT(*) AS count, id FROM `notifications` WHERE `receiver_id` = '14' AND `read` = '0' GROUP BY type, extra_id

这可能无助于回答您的问题,但您应该停止使用
mysql.*
函数。他们被弃用了。改为使用(从PHP5.1开始支持)或(从PHP4.1开始支持)。如果您不确定要使用哪一个,。这不是答案,但我在您的代码片段中看到了基本相同的代码:我将努力将其最小化,并在工作后清除干燥区域。感谢您的输入:)
SELECT type, extra_id, COUNT(*) AS count, id FROM `notifications` WHERE `receiver_id` = '14' AND `read` = '0' GROUP BY type, extra_id