我似乎无法弄清楚如何在此MySQL中订购。希望您能帮助我。
tabels:
类别
catId, catParentId, catDisplay
1 0 1
2 0 1
3 0 1
4 1 1
5 1 1
categories_translation
transId, catId, catName, catDesc, langId
1 1 Title1 Desc1 1
2 2 Title2 Desc2 1
3 3 Title3 Desc3 1
4 4 Title4 Desc4 1
5 5 Title5 Desc5 1
语言
langId, langName, langCode
1 Danish da
2 English en
我的查询:
SELECT `categories`.`catId`,
`categories`.`catParentId`,
`categories`.`catDisplay`,
`categories_translation`.`catName`,
`categories_translation`.`catDesc`,
`language`.`langCode`
FROM `categories`
INNER JOIN `categories_translation` ON `categories_translation`.`catId` = `categories`.`catId`
INNER JOIN `language` ON `language`.`langId` = `categories_translation`.`langId`
WHERE `language`.`langCode` = 'da'
现在,我得到了我想要的东西,但是有一种方法可以将孩子类别订购给父母,因此结果看起来像这样:
所需结果:
catId | catParentId | catDisplay | catName | catDesc | langCode
1 0 1 Title1 Desc1 da
4 1 1 Title4 Desc4 da
5 1 1 Title5 Desc5 da
2 0 1 Title2 Desc2 da
3 0 1 Title3 Desc3 da
我尝试过订单,但似乎可以像我想要的那样获得结果。
尝试以下内容:
ORDER BY
CASE WHEN categories.catParentId = 0 THEN
categories.catId
ELSE
categories.catParentId
END,
CASE WHEN categories.catParentId = 0 THEN
0
ELSE
categories.catId
END
对于那些没有订购的人来说,将期望的结果视为:
catId | catParentId | orderingCategory
1 0 1.0
4 1 1.4
5 1 1.5
2 0 2.0
3 0 3.0
因此,这是类别的层次结构,OP想要订购结果,以便每个父类别都遵循其子类别。
您无法获得自己喜欢的结果,因为它不可能。我看不到允许订购您想要的方式的图案或ID。
为什么标题4,标题5首先?
我看不到逻辑。也许示例不完整,也许catname和catdesc的顺序是您需要的关键。
ORDER BY catParentID, catID, ...