使用订单的意外结果

时间:2012-08-22 16:09:06

标签: php mysql

我正在制作一个通知脚本,目前有这个显示关于同一图像的多个通知,例如john likes image_name,sarah likes image_name - > 2个人喜欢image_name。

这是我到目前为止所做的。

$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;
        }

    }

}

我的数据库对于通知表看起来像这样。 http://i.minus.com/iZtrR0MNZ95Qn.png

代码的输出是,

2 New Users are following you 
5 Users Like IMAGE_ID_29
John Doe likes IMAGE_ID_50

应该在哪里

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

1 个答案:

答案 0 :(得分:1)

您当前的查询是:

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

即,您使用id字段作为count

您可能希望将其重写为:

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