English 中文(简体)
编制一个树木清单
原标题:Creating a list tree with SQLite
  • 时间:2010-10-09 21:33:29
  •  标签:
  • sql
  • sqlite

我试图用购买力平价编制一个等级清单,并编制像以下表格:

    |   itemid  |   parentid    |   name    |
    -----------------------------------------
    |   1       |   null        |   Item1   |
    |   2       |   null        |   Item2   |
    |   3       |   1           |   Item3   |
    |   4       |   1           |   Item4   |
    |   5       |   2           |   Item5   |
    |   6       |   5           |   Item6   |

清单将以无顺序清单编制,并允许这种类型的树木结构:

Item1
    |_Item3
    |_Item4
Item2
    |_Item5
        |_Item6

我用名录和固定阵列看到这一点,但我似乎无法在这种结构下工作,而且没有深度限制。

最佳回答

你们重新利用教科书设计,在结构结构数据库中储存等级数据。 这一设计称为Adjacency List,即各等级的每名节点都有oulid/code>。 外国对其直系父母至关重要。

有了这一设计,你就能够 generate树,如你描述和支持树木的任意深度。 你们已经看到这一点。

多数其他数据库(PostgreSQL、Microsoft、Oracle、IBM DB2)支持解决该问题的检索查询。

最新情况:

如果你使用旧版本,你需要另一种办法储存等级。 这方面有若干解决办法。 见我的介绍:

我通常喜欢一个设计表,但每个设计都有力量和弱点。 贵项目最能做的是,你需要哪些问题与你的数据有效。 因此,你们应该研究解决办法,选择解决办法。

问题回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...