douyun1860 2010-12-21 00:19
浏览 50
已采纳

MySQL:后续出现的次数

I have a table with four columns: id, from, to, msg.

The rows can look like this:

1 - foo - bar - hello
2 - foo - bar - zup?
3 - bar - foo - hi
4 - bar - foo - going okay
5 - bar - foo - you?

Now I wanna know how many times "bar" has tried to "talk" to "foo" without response. Like I wanna count the number of occurrences since "foo" was on the sending end.

In this example, before the next entry, it should return 3.

Is this possible in pure (My)SQL?

I'm using php on the server side.

Thanks for any tips and advice! =)

  • 写回答

3条回答 默认 最新

  • dongzuo7166 2010-12-21 00:40
    关注

    Give this a go.

    It assumes a table name of convo and the id is autoincrementing.

    This is just the mysql but the PHP calls can be added quite cleanly if you wrapped it in a function that passes in the from and to variables.

    SELECT 
      count(*) AS unreplied
    FROM
      `convo`
    WHERE
      `convo`.`from` = 'bar' AND
      `convo`.`to` = 'foo' AND
      `convo`.`id` > (SELECT
                        id
                      FROM
                        `convo`
                      WHERE
                        `convo`.`from` = 'foo' AND
                        `convo`.`to` = 'bar'
                      ORDER BY
                        `convo`.`id` DESC
                      LIMIT 1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用