我有三個表我想加入一個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