返回每一行的结果


Results Coming Back For Each Row

$friendship = mysql_query("SELECT * FROM friends WHERE accountid='$row[id]' && status='approved'") or trigger_error(mysql_error());  
while($joysong2 = mysql_fetch_array($friendship))
{
    $newnot=mysql_query("SELECT * FROM notifyme where userid='$joysong2[friendid]' ORDER BY date DESC, time DESC LIMIT 0,5") or trigger_error(mysql_error());
    while($notify = mysql_fetch_array($newnot))
    {
 }
}

大家好,让我解释一下我在这里想做什么。

我的$friendship查询正在为我们返回用户已批准与之通信的用户的结果。

因此,结果将返回为 1,3,4,5,具体取决于其他用户的用户 ID。

第二个查询$newnot是使用信息$friendship并带回表中的 5 个条目 notifyme。

问题是现在我为每个用户获得了 5 个结果。

想要的是按日期/时间顺序通知我总共只有 5 个结果。

例如。

1 posted 1/08/2015
1 posted 2/08/2015
3 posted 1/08/2015
4 posted 2/08/2015
3 posted 3/08/2015

我希望他们回来

3 posted 3/08/2015
4 posted 2/08/2015
1 posted 2/08/2015
3 posted 1/08/2015
1 posted 1/08/2015

现在虽然我的结果回来了

1 posted 1/08/2015
1 posted 2/08/2015
1 posted 1/07/2015
1 posted 1/06/2015
1 posted 1/05/2015
3 posted 3/08/2015
3 posted 1/08/2015
3 posted 1/07/2015
3 posted 1/06/2015
3 posted 1/05/2015

我想你明白我的意思。那么我该如何让它工作呢?

我想这会更好地帮助你,但请考虑一些错误,因为我没有测试它。欢迎您显示错误,直到我们修复它。将某些列名称替换为您使用的名称:

SELECT accountid, status, cnt_rows, `date` FROM friends f
LEFT JOIN (
  SELECT COUNT(*) AS cnt_rows, `date`, f_id FROM notifyme n) AS n_cnt
  ON f.id=n_cnt.f_id
)
GROUP BY n_cnt.date
WHERE status='approved'
OREDER BY `date` DESC
LIMIT 5