doudu7626 2015-03-26 13:10
浏览 21
已采纳

在单个字段中存储多个用户

I have a simple events app where there are events and users, users can join multiple events. Each event is a single row in the events table. The users have there own users table.

I'm trying to find the easiest and economical way to store all those users that are attending an event. At the moment im thinking of storing the users id in a attendees field on the events table - i would have to separate them with a comma.

I'm find with that part but im struggling to think of a way to read this field, split them into an array and then fetch the full name of each user.

Hope that makes sense.

  • 写回答

3条回答 默认 最新

  • dongtan9066 2015-03-26 13:19
    关注

    Don't do that. MySQL is a relational database model, so go ahead and use relations. In that case, create a new attendance table with two columns: user id and event id.

    It will make your tasks even easier; if you need to find all events for one user it's as simple as finding all users for one event.

    For example, to get all attendees' names, it would be something like

    SELECT u.name
    FROM user u
        INNER JOIN attendance a ON a.userid = u.id
        INNER JOIN event e ON a.eventid = e.id
    WHERE e.id = <youreventid>;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程