[英]MySQL ORDER BY with multiple INNER JOINs
I can't seem to figure out how to order by in this MySQL select. 我似乎无法弄清楚如何在这个MySQL选择中订购。 I hope you can help me out.
我希望你能帮助我。
Tabels: Tabels:
categories 类别
catId, catParentId, catDisplay
1 0 1
2 0 1
3 0 1
4 1 1
5 1 1
categories_translation 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
language 语言
langId, langName, langCode
1 Danish da
2 English en
My query: 我的查询:
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'
Now, I get returned what I want, but is there a way to order the child categories to their parents, so the result looks like this: 现在,我得到了我想要的东西,但有没有办法将子类别命令给他们的父母,所以结果如下:
Desired result: 期望的结果:
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
I've tried order by, but can seem to get the results like I want. 我已经尝试过了,但似乎可以得到我想要的结果。
You can't get the results you like, because its not possible. 你不能得到你喜欢的结果,因为它不可能。 I can't see a pattern or an id which allow to order the way you want.
我看不到允许按照你想要的方式订购的模式或id。
Why title 4, title 5 go first? 为什么标题4,标题5先行?
I can't see the logic. 我看不出逻辑。 Maybe the example is incomplete, maybe the order of CatName and CatDesc is the key you need.
也许这个例子不完整,也许CatName和CatDesc的顺序是你需要的关键。
Try the following: 请尝试以下方法:
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
For those that don't get the ordering it would be easier to think of the desired result as: 对于那些没有得到订单的人来说,更容易想到所需的结果:
catId | catParentId | orderingCategory
1 0 1.0
4 1 1.4
5 1 1.5
2 0 2.0
3 0 3.0
So it's a hierarchy of categories, OP wants to order the result so that each parent categories are followed by their child categories. 因此,它是类别的层次结构,OP希望对结果进行排序,以便每个父类别后跟其子类别。
ORDER BY catParentID, catID, ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.