我有三个表我想加入一个MySQL查询来检查用户是否有权下载照片。每张照片都有自己的访问权限,可以是以下任一权限。加入三个表来创建一个访问授权列表
一级 - 任何用户都可以下载照片。
二级 - 只有有权访问的用户才能下载照片。
表1:FILES
FILE_ID is AUTO_INCREMENT
USER_ID is the identifier of the user that owns the file.
FILE_NAME is just the name of the photo.
FILE_ACCESS is the access rights to the 2 levels noted above.
FILE_ID | USER_ID | FILE_NAME | FILE_ACCESS
1 | 3 | 1279141923.jpg | 1
2 | 3 | 1279141925.jpg | 1
3 | 3 | 1279141927.jpg | 2
4 | 4 | 1279141929.jpg | 1
5 | 4 | 1279141931.jpg | 2
6 | 3 | 1279141933.jpg | 2
表2:用户
USER_ID is AUTO_INCREMENT
USER_NAME is just the name of the file owner.
USER_ID | USER_NAME
1 | jack
2 | jill
3 | john
4 | mike
表3:ACCESS
ACCESS_ID is AUTO_INCREMENT
USER_ID is the identifier of the user that owns the file.
ALLOW_ID is the identifier of the user that has access all the file uploaded by user.
ACCESS_ID | USER_ID | ALLOW_ID
1 | 3 | 1
2 | 3 | 4
用户杰克访问下载照片1279141923.jpg
,1279141925.jpg
和1279141927.jpg
那John已经上传,而Jill只能访问1279141923.jpg
和1279141925.jpg
Jack可以访问所有三个文件,因为文件1和2具有访问级别1,同时他还可以访问下载文件3,看到John已授予他完全访问John上传的所有文件的权限。
SELECT a.file_name, b.user_name FROM files AS a
JOIN users AS b ON a.user_id = b.user_id
WHERE a.file_access = '1'
这MySQL查询给了我当吉尔签署以下。
1279141923.jpg, john (owner name)
1279141925.jpg, john
1279141929.jpg, mike
我现在正在引进的访问表到这个查询,因此,如果吉尔在她签显示如果杰克签字,他的结果与上面相同,他会得到以下结果。
1279141923.jpg, john (owner name)
1279141925.jpg, john
1279141927.jpg, john
1279141929.jpg, mike
1279141933.jpg, john
我希望我解释这个权利,因为我确实需要帮助最后一部分。我不太清楚如何将其添加到当前的查询中。我可以使用多个查询来做到这一点 - 但我更喜欢如果可能的话将它作为一个整体。
感谢您的清理。这看起来更容易阅读。 – Tim 2012-01-07 22:21:17