douba4933 2012-08-27 21:18
浏览 6
已采纳

你能帮我制定一个SQL查询来加入3个表吗? [关闭]

I'm not sure how to do this so I'm asking it here...

I have 3 tables in my database with structure like this..

table:contact ----- rows: contact_id, firstname

table:groups ------rows: group_id, group_name

table:groupmembership ------ rows: contact_id, group_id

The association between between "contact_id" and "group_id" in the table "group_membership" reflects a contact that is in a particular group.

What I need to do is take a contact_id, and display which groups he is a member of in a dropdown list. I'll eventually use this dropdown list to post the group_id to a page that diplays the group info.

Can anyone help me with how to formulate the MySQL query?

  • 写回答

1条回答 默认 最新

  • dphdh395195 2012-08-27 21:20
    关注

    If you want to select by user_id:

    SELECT group_name, group_id
    FROM groupmembership gm
    INNER JOIN groups g ON g.group_id = gm.group_id
    WHERE gm.contact_id = 4711
    

    If you want to select by user's firstname:

    SELECT group_name, group_id
    FROM groupmembership gm
    INNER JOIN groups g ON g.group_id = gm.group_id
    INNER JOIN contact c ON c.contact_id = gm.contact_id
    WHERE c.firstname LIKE 'TheHe';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c