nicky
12-05-2011, 03:13 PM
I have a user login system that allows for files to be uploaded into specific folders. The problem I am experiencing is that when you go to view the files that have been uploaded, if a file was categorized into two folders, two separate entries for the same file will appear.
files
+------+---------+-------+
| file_id | name __| author |
+------+---------+-------+
| 28 __| logo.png | 1 ____|
+------+---------+-------+
files_folders
+------+----------+
| file_id | folder_id |
+------+----------+
| 28 ___| 3 ______|
| 28 ___| 1 ______|
+------+----------+
folders
+---------+------------+-----------------+
| folder_id | folder_name | parent_folder_id |
+---------+------------+-----------------+
| 1 ______| ABC ______| 0 _____________|
| 3 ______| XYZ ______| 0 _____________|
+---------+------------+-----------------+
I need to select all the files and display the folder it's in, but I cannot have duplicate entries if the file is categorized into two or more folders.
My query so far is...
SELECT * FROM files LEFT JOIN files_folders ON files.file_id = files_folders.file_id LEFT JOIN users on files.author = 1 LEFT JOIN folders on files_folders.folder_id = folders.folder_id ORDER BY files.file_id DESC
I tried using GROUP BY but that didn't work.
I would appreciate any and all help. This is giving me a headache!
files
+------+---------+-------+
| file_id | name __| author |
+------+---------+-------+
| 28 __| logo.png | 1 ____|
+------+---------+-------+
files_folders
+------+----------+
| file_id | folder_id |
+------+----------+
| 28 ___| 3 ______|
| 28 ___| 1 ______|
+------+----------+
folders
+---------+------------+-----------------+
| folder_id | folder_name | parent_folder_id |
+---------+------------+-----------------+
| 1 ______| ABC ______| 0 _____________|
| 3 ______| XYZ ______| 0 _____________|
+---------+------------+-----------------+
I need to select all the files and display the folder it's in, but I cannot have duplicate entries if the file is categorized into two or more folders.
My query so far is...
SELECT * FROM files LEFT JOIN files_folders ON files.file_id = files_folders.file_id LEFT JOIN users on files.author = 1 LEFT JOIN folders on files_folders.folder_id = folders.folder_id ORDER BY files.file_id DESC
I tried using GROUP BY but that didn't work.
I would appreciate any and all help. This is giving me a headache!