如何有效地选择具有父匹配标准的所有行?

时间:2014-08-21 17:02:23

标签: sqlite

从具有列ID,IDofParent和Flavor的SQLite表中,如何使用max。效率我可以返回任何父母有Flavor = Lemon的记录吗?

1 个答案:

答案 0 :(得分:1)

如果你有SQLite 3.8.3或更高版本,可以通过递归common table expression来完成:

WITH RECURSIVE LemonsAndChildren AS (
    SELECT * FROM ATable WHERE Flavour = 'Lemon'
    UNION ALL
    SELECT Child.* FROM ATable AS Child
        JOIN LemonsAndChildren AS Parent ON Child.IDofParent = Parent.ID
)
SELECT * FROM LemonsAndChildren