doudou7361 2015-05-27 13:21
浏览 74

检查MySQL表中是否存在值,然后选择[Laravel 5]

Here is my pivot table project_group:

+-----+----------+------------+----------+---------+
| ids | group_id | project_id | admin_id | user_id |
+-----+----------+------------+----------+---------+
|   4 |      115 |          1 |        1 | [3,4,5] |
|   5 |      115 |          2 |        1 | [5,2,1] |
|   6 |      115 |          3 |        1 | [1,3,6] |

This table represent group linked to the projects....user_id is which users can see projects/group... Is there any way to display correct projects/group only to the users defined in user_id?

Also content in user_id field can be changed....

  • 写回答

1条回答 默认 最新

  • duanshan2988 2015-05-27 13:43
    关注

    The best way to handle this would be to first normalize your database. Storing comma separated lists in a cell is allowed, but generally bad practice, as explained in this question.

    If you can have multiple users per project, you should have a linking table with a column for project and a column for user, like this:

    project_users:
    | project_id | user_id |
    

    and you can make (project_id, user_id) a composite primary key.

    That way, you can select the users for a project (say, project 1) like this:

    SELECT user_id
    FROM project_users
    WHERE project_id = 1;
    

    Once you have these, you can display the project data only to users whose id is returned in the above list.

    I have built an SQL Fiddle that helps demonstrate this visually, if it helps.

    It is good to note that this proper normalization gives the opportunity to a lot of useful data as well, as it becomes easier to search for users by project, but also you can search for project information based on a user.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集