douyue1481 2014-10-20 13:10
浏览 84
已采纳

MySQL关系数据库设计

I have seen a question on this forum that I can relate with, but I can't apply the answers to my question.

Here it goes:

  1. I have a memberlist table (id, name, number) I'll just make the columns short.
  2. Next, I have an events table (id, eventName, description)

Now, 1. each member in the memberlist can join events as many as he wants. 2. each events in the events table can have members without limits (okay, say 1k members, like that or whatever).

What I have now is an event table that has a column named: "joiners" which will contain the id of a certain joiner/member. But I believe I'm wrong because how can a certain event handles many joiner's id?

  • 写回答

5条回答 默认 最新

  • douxuanyi2813 2014-10-20 13:17
    关注

    I would rename memberlist into members to make your table naming more consistent. Or events into eventlist. Which ever you like more.

    Then you want to define a many to many relation between members and events. This is done through an intermediate table which will reference both:

    create table eventmembers (
        id int unsigned not null primary_key auto_increment,
        member_id int unsigned not null references members(id),
        event_id int unsigned not null references events(id)
    )
    

    I'm assuming that on your memebers and events you already have id fields which are set to be primary keys.

    If you want to get all events attended by a specific user you can then do

    select events.*
    from events
    left join eventmembers
      on events.id = eventmembers.event_id
    where
        member_id = ?
    

    and get all the members in an event:

    select members.*
    from members
    left join eventmembers
      on members.id = eventmembers.member_id
    where
        event_id = ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题