MySQL Stored Procedure Design Problem V2. Recusion or Hierarchy?

流过昼夜 提交于 2019-12-11 05:47:10

问题


Suppose we have a table named Connected, with column node_1 and node_2, both integer type, and some data in it.

It looks like this:

`node_1`  `node_2` 
    A        B
    C        D
    B        C
    B        F
    C        W
    D        N
    D        Q

        .
        .
        .  Much much much more
        .  

What Im trying to do is expand this table, and then collect all relations. For instance, if we can travel from 'A' to 'Q' with path 'A-B-C-D-Q' ( defined by the old table, 'A-B, B-C, C-D, D-Q'), we say that 'A' and 'Q' can be connected, and insert this pair into new table.

Such that, the result table should looks like this:

`node_1`  `node_2` 
    A        B
    A        C
    A        D
    A        F
    A        N
    A        Q
    A        W
    B        C
    B        D
    B        F
    B        N
    B        Q    
    B        W
    C        D
    C        N
    C        Q
    C        W
    D        Q
    D        N                      

I appreciate all helps !

Change the word "Node" to "Group", "connected" to "contains", does it sound more reasonable? Im Sorry for my gramma or any other language related problemssss :)


回答1:


Same as my other solution, just use ASCII() around the character to the its ascii value in the SELECT.

MySQL Stored Procedure Design Problem. Recusion or Hierarchy?

PS. due to our conversation in the other question. Im getting the impression your really content of finding a imperative solution for this. Remember, SQL is a declarative language.



来源:https://stackoverflow.com/questions/6603175/mysql-stored-procedure-design-problem-v2-recusion-or-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!