我需要在下表中找到父母和孩子。我试过下面的查询。但它会抛出错误
[Err] 1235 - 此版本的MariaDB尚未支持' LIMIT& IN / ALL / ANY / SOME子查询
我需要找到所有父母,直到它回复null。请帮我找到。
SELECT id, name
FROM categories
WHERE id IN (SELECT id FROM categories WHERE parentid IS NULL LIMIT 1)
UNION ALL
SELECT cat.id, cat.name
FROM categories as cat,
categories
WHERE categories.id = cat.parentid
`
答案 0 :(得分:2)
您可以通过以下查询来实现此目的:此查询显示具有其父名称的所有子项。这导致多行。
SELECT child.id
,child.NAME
,parent.NAME
FROM categories AS parent
LEFT JOIN categories AS child ON child.parentid = parent.Id
顺序可以这样,但没有经过测试
SELECT child.id
,child.NAME
,parent.NAME
FROM categories AS parent
LEFT JOIN categories AS child ON child.parentid = parent.Id
ORDER BY coalesce(parent.id, child.id)
,parent.id IS NOT NULL
,child.id