簡體   English   中英

如何連接3個表,其中一個表映射其他兩個表的ID?

[英]How to joint 3 table where one table map id from the two other tables?

我試圖在頁面上顯示鏈接到登錄用戶的每個文件,以顯示有關該文件,用戶以及該文件的有關人員的相關信息。

以下是表格:

staff     users     map_file_users   files       category
id        id        id               id          id
pwd       name      file_id -------> ...         name
user_id-> ...  <--- user_id          category -->...

為此,我使用了以下代碼,該代碼顯示了登錄用戶檢索到的我需要的列后發布的每個文件:

SELECT info_client.name        AS client_name,
       info_client.surname     AS client_surname,
       info_staff.surname      AS staff_surname,
       info_staff.name         AS staff_surname,
       files.creation_date     AS file_creation_date,
       files.modification_date AS file_modification_date,
       files.size              AS file_size,
       files.type              AS file_type,
       category.name           AS file_category
FROM files
         INNER JOIN clients client      ON files.about_client = client.id
         INNER JOIN users info_client   ON info_client.id = client.user_id
         INNER JOIN staff staff         ON files.published_by = staff.user_id
         INNER JOIN users info_staff    ON info_staff.id = staff.user_id
         INNER JOIN categories category ON files.category = category.id
WHERE files.published_by = :id;

我沒有利用表“ map_file_staff”來顯示鏈接到登錄用戶的所有文件。通過簡單地將文件ID和用戶ID插入到該“ map_file_staff”表中,我也可以輕松地允許/禁止某人訪問該文件。它。

希望一切都清楚!

答案是:

select staff.surname           as staff_surname,
       staff.name              as staff_name,
       files.title             as file_title,
       files.creation_date     as file_creation_date,
       files.modification_date as file_modification_date,
       files.size              as file_size,
       files.type              as file_type,
       category.name           as file_category,
       client.name             as client_name,
       client.surname           as client_surname

from map_file_user
         INNER JOIN users as staff on staff.id = map_file_user.user_id
         INNER JOIN files on files.id = map_file_user.file_id
         INNER JOIN categories category on files.category = category.id
         INNER JOIN clients c on files.about_client = c.id
         INNER JOIN users as client on client.id = c.id
WHERE map_file_user.user_id = 2;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM