dssk35460 2016-07-21 20:24
浏览 269
已采纳

mySql查询中间表

i try to find out if a conversation between two users exist but i can't imagine how to query this in mysql.

My tables are:

users
-----
id
name

conversations
-------------
id

conversation_user
-----------------
user_id
conversation_id

For example, i have two users and one conversation so in my conversation_user table i have

user_id    conversation_id
1          1
2          1

How to check if this relationship exist or not?

thank you in advance.

  • 写回答

2条回答 默认 最新

  • dsubq24666 2016-07-21 20:40
    关注

    You can use join for that:

    SELECT
        c1.*, c2.*
    FROM
        conversation_user c1
    INNER JOIN
        conversation_user c2
    ON
        c1.conversation_id = c2.conversation_id
    WHERE
        c1.user_id != c2.user_id;
    

    You can take a look at this SQLFiddle Example that is a bit more detailed. It shows an example of bringing the name of the users and one that filters by conversation.

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

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办