Reputation: 134
I have 2 tables folder structure like this in MySQL:
folders Table:
|folder_id | folder_parent_id | folder_name | is_active|
+----------+------------------+-------------+----------+
| 1 | NULL | Desktop | 1 |
| 2 | NULL | Downloads | 1 |
| 3 | 2 | Movies | 1 |
| 4 | 2 | Musics | 0 |
| 5 | 1 | Trash | 1 |
| 6 | NULL | Systems | 1 |
| 7 | NULL | Locals | 0 |
documents Table:
|document_id | folder_id | document_name | document_status |
+------------+------------+---------------+-------------------+
| 1 | NULL | Invoice | approved |
| 2 | 3 | Subtitle | approved |
| 3 | 4 | Lyrics | not_approved |
| 5 | 6 | ReadME | approved |
| 6 | 2 | Script | approved |
So basically this two tables are for my file manager system,i need to retrieve the folder and documents based on the folder_parent_id
, this column is to mark if a folder is inside another folder so the another folder will be the parent. if the folder_parent_id
is NULL
it means the folder is in the root of the directory. As for the document it can be inside another folder or in the root directory (folder_id
is null). What i want to achieve is to get a select data like this:
for example in case of folder_parent_id is NULL AND is_active = 1 :
|folder_id | folder_name | document_id |document_status|document_name|
|----------+-------------+--------------+---------------+-------------+
| 1 | Desktop | NULL | NULL | NULL |
| 2 | Downloads | NULL | NULL | NULL |
| 6 | Systems | NULL | NULL | NULL |
| NULL | NULL | 1 | approved | Invoice |
for example in case of folder_parent_id
is 2 AND is_active
= 1 :
|folder_id | folder_name | document_id |document_status|document_name|
|----------+-------------+--------------+---------------+-------------+
| 3 | Movies | NULL | NULL | NULL |
| 4 | Musics | NULL | NULL | NULL |
| NULL | NULL | 6 | approved | Script |
it will select all the folders and files with column like that
Upvotes: 0
Views: 50
Reputation: 57573
If I understand what you want, I think you need something like this:
SELECT folder_id, folder_name,
NULL AS document_id, NULL as document_status, NULL as document_name
FROM folders
WHERE folder_parent_id = your_number AND is_active = 1
UNION
SELECT NULL as folder_id, NULL as folder_name,
document_id, document_status, document_name
FROM documents
WHERE folder_id = your_number AND is_active = 1
Upvotes: 1