Je suis en train de faire une demande MySQL pour récupérer un menu à 2 niveaux (parents et enfants) ...MySQL 2 Niveau menu Requête
Il y a seulement 1 table, bien sûr:
idCategory | Title | idCategoryParent | DisplayOrder
1 | cat1 | NULL | 1
2 | sub-cat1 | 1 | 1
3 | sub-cat2 | 1 | 2
4 | cat2 | NULL | 2
5 | sub-cat3 | 4 | 1
6 | sub-cat4 | 4 | 2
7 | cat3 | NULL | 3
Je suis à la recherche de ces résultats:
titleCat | titleSubCat | idCategory
cat1 | sub-cat1 | 1
cat1 | sub-cat2 | 1
cat2 | sub-cat3 | 4
cat2 | sub-cat4 | 4
cat3 | NULL | 7
ou quelque chose comme ça serait bien aussi:
cat1 | null | 1
cat1 | sub-cat1 | 1
cat1 | sub-cat2 | 1
etc..
J'ai essayé avec quelque chose comme:
SELECT subcat.title as catTitle, cat.title as parentTitle, subcat.idCategory as catIdCategory, subcat.idCategoryParent
FROM `test_category` as cat
RIGHT OUTER JOIN test_category as subcat ON cat.idCategory=subcat.idCategoryParent
Ne fonctionne pas mal, mais je lutte en essayant de commander les dossiers ...
Voici le Dump SQL si vous voulez l'essayer:
--
-- Table structure for table `test_category`
--
CREATE TABLE IF NOT EXISTS `test_category` (
`idCategory` int(11) NOT NULL AUTO_INCREMENT,
`idCategoryParent` int(11) DEFAULT NULL,
`title` varchar(20) NOT NULL,
`order` int(11) NOT NULL,
PRIMARY KEY (`idCategory`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
--
-- Dumping data for table `test_category`
--
INSERT INTO `test_category` (`idCategory`, `idCategoryParent`, `title`, `order`) VALUES
(1, NULL, 'cat1', 1),
(2, 1, 'sub-cat1', 1),
(3, 1, 'sub-cat2', 2),
(4, NULL, 'cat2', 2),
(5, 4, 'sub-cat3', 1),
(6, 4, 'sub-cat4', 2),
(7, NULL, 'cat3', 3);
Merci! :)
Il devrait être commandé par "DisplayOrder" mais il semble fonctionner avec lui aussi! Merci! :) – mrmuggles
Oh, je passais par l'exemple de sortie, qui semblait être trié par titre. –
np, je n'étais pas vraiment clair à ce sujet;) – mrmuggles