dongnaosuan5407 2015-06-05 02:33
浏览 50
已采纳

使用MySQL / PHP中的关系数据库过滤信息

I'm working on my first project (a project management system) to understand PHP and MySQL better. I currently have 3 tables in my database, one that lists all the projects ('projects' table), one that stores the users username and password ('users' table) and then a third, small 'permissions' table that is meant to filter out what projects a user can see from the projects table.

The third 'permissions' table consists of an unique id, and then a "user_id" and a "project_id" in the other two columns. The "user_id" references the id of the user in the "users" table, and the "project_id" references the id of a project in the projects table.

Note that more than one user can have access to a single project, that's why there isn't just a single column for this in the projects table, and I read that using comma separated lists for the multiple usernames is a bad way to go about it. As such I have set up foreign keys on "user_id" and "project_id" that reference the id of each accordingly.

Now I'm not sure if this is a good way to set things up but it seems okay in my head.

Really what I want to do is retrieve all the projects a certain user has access to. So something kind of like (but obviously works):

SELECT * FROM projects WHERE user_id = 3;

Is there a simpler way to do this? Something to do with the join command but I could never get it right.

Thanks in advance!

EDIT:

Apologies for the ambiguity, gets a bit tricky to explain.

Essentially I just want a way to filter what rows are displayed from the projects table by the user_id in the users table (note again more than one user can access a project so I can just put a "user_id" column in the projects table and leave out the third table altogether).

My tables are pretty much like so:

projects:

|id|project_title|project_notes|project_status|
|1 |A Project    |Some Notes   |Finished      |
|2 |A 2nd Project|Some more... |In Progress   |

users:

|id|username|password|
|1 |johndoe |*********|
|2 |benhill |*********|

permissions:

|id|user_id|project_id|
|1 |1      |2         |
|2 |2      |2         |

user_id = id from users table. project_id = id from projects table.

Then in HTML/PHP I only want to retrieve the projects that a user has access too.

  • 写回答

2条回答 默认 最新

  • dosryjij88555 2015-06-05 02:53
    关注

    Unfortunately your question isn't very clear but I assume you want to have a single query to find all project that a particular user has access to, you can simply join up the tables with the id as such:

    SELECT * from projects INNER JOIN permissions ON 
    projects.id = permissions.project_id WHERE 
    permissions.user_id = 3
    

    Assume the user you are looking for has id of 3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?