问题
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