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 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据