duanbi3385 2015-12-10 19:19 采纳率: 100%
浏览 49
已采纳

相同的id,但只获得1个结果

I'm about to make a message system. Instead of making 2 tables like conversations and messages, I would just like to have messages. There would then be a lot of rows with the same user id's like this:

id     to_id     from_id     message
1      1         2           text...
2      1         3           text...
3      2         1           text...
4      1         2           text...
5      1         2           text...
6      1         2           text...

The thing is, that I want to get one of each conversation. If id 1 writes to id 2, then thats 1 conversation, but if id 2 writes a reply to id 1, then it would STILL be only 1 conversation.

In the above example, there is only 2 conversations (id 1 to id 3) and (id 1 to id 2 and reverse).

How can i Achieve this. My idea is that there must be something simpler than DISTINCT.

  • 写回答

2条回答 默认 最新

  • dongmeiwei0226 2015-12-10 19:45
    关注

    I might go for a related table but to keep it simple, just add the column conv_id.

    When a message is created, create a new conv_id and add it to the table row with the other data. When a message is replied to just use that conv_id for the new row.

    When you want to retrieve conversation(s) either select WHERE conv_id=x and/or use a GROUP BY conv_id.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分