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条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图