I use this query to get the number of documents the folder contains:
$query = "
SELECT
*,
count(*) as number_documents
FROM
table_documents
LEFT JOIN
table_folders ON folder_id = document_folder_id
GROUP BY
document_folder_id
ORDER BY
number_documents DESC
";
I can't get the folders with 0 documents, how do i get the folders to have:
FOLDER 1 - 100 documents
FOLDER 2 - 35 documents
FOLDER 3 - 0 documents
TABLES:
**table_folders**
folder_id
folder_description
folder_active
**table_documents**
document_id
document_folder_id
document_description
document_file
I think you have to sum the documents from the document folder. Try the query
SELECT d.document_folder_id
, SUM(d.documents) as number_documents
FROM table_documents d
LEFT
JOIN table_folder f
ON f.folder_id = d.document_folder_id
GROUP BY d.document_folder_id
since you dint clearly explained about the table structure .I have created table locally and tried , Try the query which i have tried locally
create table table_documents(id int,doc int);
insert into table_documents values
(1,100),
(2,35),
(3,0),
(1,101),
(2,36),
(3,0);
create table table_folder(id int);
insert into table_folder values
(1),(2),(3);
SELECT d.id
, SUM(d.doc) as number_documents
FROM table_documents d
LEFT
JOIN table_folder f
ON f.id = d.id
GROUP
BY d.id;// query for your output
This will work:
SELECT
*,
sum(if (document_folder_id is null,0,1)) as number_documents
FROM
table_folders
LEFT JOIN
table_documents ON document_folder_id = folder_id
GROUP BY
folder_id
ORDER BY
number_documents DESC