gpt4 book ai didi

php - 查询获取树上的类别和产品

转载 作者:行者123 更新时间:2023-11-30 21:45:05 26 4
gpt4 key购买 nike

我有类别表:

enter image description here

和产品表:

enter image description here

我想查询得到一棵同时包含类别和产品的树,例如:如果类别的最后一级是类别的产品,则下图相同:

enter image description here

最佳答案

更新:

谢谢 @bradbury9

我尝试 JOIN 2 表并使用 UNION ALL,它对我有用:

SELECT *
FROM
(
(
SELECT NULL AS content_id, NULL AS question, NULL AS answer, node.*,
(
SELECT COUNT(parent1.id) - 1
FROM faq_categories AS node1,
faq_categories AS parent1
WHERE node1.lft BETWEEN parent1.lft AND parent1.rgt AND node1.id = node.id
GROUP BY node1.name
ORDER BY node1.lft
) AS level
FROM `faq_contents` as content
LEFT JOIN `faq_categories` AS node ON node.`id` = content.`category_id`
LEFT JOIN `faq_categories` AS parent ON parent.`id` = content.`category_id`
WHERE node.lft BETWEEN parent.lft AND parent.rgt
GROUP BY node.id

)
UNION ALL
(
SELECT content.id AS content_id, content.question, content.answer, node.*,
(
CASE WHEN content.id IS NULL THEN
(
SELECT COUNT(parent1.id) - 1
FROM faq_categories AS node1,
faq_categories AS parent1
WHERE node1.lft BETWEEN parent1.lft AND parent1.rgt AND node1.id = node.id
GROUP BY node1.name
ORDER BY node1.lft
)
ELSE 3 END
) AS level
FROM `faq_contents` AS content
RIGHT JOIN `faq_categories` AS node ON node.`id` = content.`category_id`
)
) node
ORDER BY lft

关于php - 查询获取树上的类别和产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49811918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com