CREATE TABLE `folder_s` ( `folder_id` int(10) NOT NULL AUTO_INCREMENT, `folder_name` varchar(10) DEFAULT NULL, `folder_type` varchar(10) DEFAULT NULL, `creation_date` varchar(20) DEFAULT NULL, `author` varchar(20) DEFAULT NULL, `parent_id` int(11) NOT NULL, PRIMARY KEY (`folder_id`), KEY `bbbb_idx` (`parent_id`), CONSTRAINT `FKparent_folder` FOREIGN KEY (`parent_id`) REFERENCES `folder_s` (`folder_id`) ON DELETE CASCADE ON UPDATE CASCADE CREATE TABLE `document_s` ( `document_id` int(10) NOT NULL AUTO_INCREMENT, `document_name` varchar(20) DEFAULT NULL, `document_type` varchar(20) DEFAULT NULL, `creation_date` varchar(20) DEFAULT NULL, `parent_id` int(10) DEFAULT NULL, PRIMARY KEY (`document_id`), KEY `aaa` (`parent_id`), CONSTRAINT `aaa` FOREIGN KEY (`parent_id`) REFERENCES `folder_s` (`folder_id`) ON DELETE CASCADE ON UPDATE NO ACTION Folder_s elements are folders that can contain other folders as well as documents from document_s.
Thus, if I have data for example
INSERT INTO `folder_s` VALUES (1,'/','/','2017/02/21','admin',1), (2,'root','root','2017/02/21','admin',1), (3,'Folder A','Dosar','2017/02/26 23:27','nolek',2), (4,'Folder B','Catalog','2017/02/26 23:27','nolek',2), (5,'Folder C','Pachet','2017/02/26 23:27','nolek',3), (6,'Folder D','Dosar','2017/02/26 23:28','nolek',4); (7,'Folder E','Dosar','2017/02/26 23:28','nolek',5); (8,'Folder F','Dosar','2017/02/26 23:28','nolek',6); INSERT INTO `document_s` VALUES ('1', 'DocumentA', 'Dosar', '2017/02/26 23:27', '2'), ('2', 'DocumentB', 'DocExt', '2017/02/26 23:27', '2'), ('3', 'DocumentC', 'Dosar', '2017/02/26 23:27', '5'), ('4', 'DocumentD', 'DocExt', '2017/02/26 23:27', '5'), ('5', 'DocumentE', 'Dosar', '2017/02/26 23:27', '6'), ('6', 'DocumentF', 'DocExt', '2017/02/26 23:27', '6'), ('7', 'DocumentG', 'Dosar', '2017/02/26 23:27', '7'), ('8', 'DocumentH', 'DocInt', '2017/02/26 23:27', '8'); All that is in the root is a zero level. And you need to make a selection of documents of the 3rd level, that is
Root FolderA FolderC FolderE DocumentG DocumentC DocumentD FolderB FolderD FolderF DocumentH DocumentE DocumentF DocumentA DocumentB In this tree it will be
DocumentC DocumentD DocumentE DocumentF Tell me how it can be done in MySQL and how to do it at all.
insert into tmp(id, level) select id,N from folder_s where parent_id in(select id from tmp where level=N-1)and finally select documents whose parent is in tmp with the appropriate level - Mike