dotcraq3249 2015-03-01 23:11
浏览 17

结果回到每一行

$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))
    {

 }
}

Hi guys let me explain what I am trying to do here.

My $friendship query is giving us back a result of the users who the user has approved to communicate with.

So the result would come back as 1,3,4,5 depending on the other users user id.

The second query $newnot is using the information $friendship and bringing back 5 entries in the table notifyme.

Problem is right now that I'm getting 5 results for each user.

What I wanted was only 5 results in total from notify me in date/time order.

eg.

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

I would want them back as

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

right now though my results are coming back as

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

I guess you get what I mean. So how do I get this to work?

  • 写回答

1条回答 默认 最新

  • dougu4704 2015-08-05 19:58
    关注

    I suppose this will help you better, but consider some error because I didn't test it. You are welcome to show errors until we fix it. replace some column names with the one you use:

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?