2012-07-20 69 views
0

嗨,我正在使用codeigniter。我有这样的桌子。如何从数据库中获取这些值,codeigniter活动记录

PreferenceID | PreferencParentID | Value 
    1   |  0   | Fashion 
    2   |  0   | music 
    3   |  1   | Men's fashion 
    4   |  1   | Women's fashion 
    5   |  2   | Rock music 
    6   |  3   | shirts 

如果我通过PreferenceID = 1我想记录Fashion,Men's fashion,Women's fashion,shirts

,如果我通过PreferenceID = 2我想记录music ,Rock music

  • 我想在所有级别的所有子值当我通过一个父ID。 这里如果我通过PreferenceID = 1它是Fashoin,它有两个孩子,Men's fashionWomen's fashionand men's fashionshirts的父值。

这是层次结构。它像一个树状结构,并进入N个级别,请帮我

enter image description here

目前我做的是通过prefernceID并得到所有下一级孩子的,

例如: - 我传递prefernceID = 1并获得唯一Men's fashionWomen's fashion :(

请帮助。在此先感谢

+1

请参阅本http://stackoverflow.com/questions/169817/is-it-possible-to-query-a- tree-structure-table-in-mysql-in-a-single-query-to-an – Madhivanan 2012-07-20 06:56:59

+0

你有没有任何最大的深度,你打算有?就像3或4一样,或者你打算如何尽可能深入? – 2012-07-20 11:25:32

回答

0

有没有办法做到这一点通过AR类。试着这样的:

$sql = 'SELECT `PreferenceID`, `PreferencParentID`, `Value` 
     FROM `YourTableName` 
     WHERE 1 IN(`PreferenceID`, `PreferencParentID`)'; 
$this->db->query($sql); 

(假设你使用MySQL)

+0

实际上OP是要求获取1的子子女,也就是包括来自ID 1的上述数据的“衬衫”。 – TigerTiger 2012-07-20 11:07:52

0

也许你应该使用MySQL程序或适配器视图。

像你这样的实例数据后:

mysql> create table treeNodes 
    -> (
    -> id int primary key, 
    -> nodename varchar(20), 
    -> pid int 
    ->); 
Query OK, 0 rows affected (0.09 sec) 
mysql> select * from treenodes; 
+----+----------+------+ 
| id | nodename | pid | 
+----+----------+------+ 
| 1 | A  | 0 | 
| 2 | B  | 1 | 
| 3 | C  | 1 | 
| 4 | D  | 2 | 
| 5 | E  | 2 | 
| 6 | F  | 3 | 
| 7 | G  | 6 | 
| 8 | H  | 0 | 
| 9 | I  | 8 | 
| 10 | J  | 8 | 
| 11 | K  | 8 | 
| 12 | L  | 9 | 
| 13 | M  | 9 | 
| 14 | N  | 12 | 
| 15 | O  | 12 | 
| 16 | P  | 15 | 
| 17 | Q  | 15 | 
+----+----------+------+ 
17 rows in set (0.00 sec) 

树级别是:

1:A 
    +-- 2:B 
    | +-- 4:D 
    | +-- 5:E 
    +-- 3:C 
     +-- 6:F 
      +-- 7:G 
8:H 
    +-- 9:I 
    | +-- 12:L 
    | | +--14:N 
    | | +--15:O 
    | |  +--16:P 
    | |  +--17:Q 
    | +-- 13:M 
    +-- 10:J 
    +-- 11:K 

,你可以创建一个函数getChildLst获取包含所有子节点的字符串。

mysql> delimiter // 
mysql> 
mysql> CREATE FUNCTION `getChildLst`(rootId INT) 
    -> RETURNS varchar(1000) 
    -> BEGIN 
    -> DECLARE sTemp VARCHAR(1000); 
    -> DECLARE sTempChd VARCHAR(1000); 
    -> 
    -> SET sTemp = '$'; 
    -> SET sTempChd =cast(rootId as CHAR); 
    -> 
    -> WHILE sTempChd is not null DO 
    ->  SET sTemp = concat(sTemp,',',sTempChd); 
    ->  SELECT group_concat(id) INTO sTempChd FROM treeNodes where FIND_IN_SET(pid,sTempChd)>0; 
    -> END WHILE; 
    -> RETURN sTemp; 
    -> END 
    -> // 
Query OK, 0 rows affected (0.00 sec) 

mysql> 
mysql> delimiter ; 

那么我们可以用FIND_IN_SET功能找到的所有值:

mysql> select getChildLst(1); 
+-----------------+ 
| getChildLst(1) | 
+-----------------+ 
| $,1,2,3,4,5,6,7 | 
+-----------------+ 
1 row in set (0.00 sec) 

mysql> select * from treeNodes 
    -> where FIND_IN_SET(id, getChildLst(1)); 
+----+----------+------+ 
| id | nodename | pid | 
+----+----------+------+ 
| 1 | A  | 0 | 
| 2 | B  | 1 | 
| 3 | C  | 1 | 
| 4 | D  | 2 | 
| 5 | E  | 2 | 
| 6 | F  | 3 | 
| 7 | G  | 6 | 
+----+----------+------+ 
7 rows in set (0.01 sec) 

mysql> select * from treeNodes 
    -> where FIND_IN_SET(id, getChildLst(3)); 
+----+----------+------+ 
| id | nodename | pid | 
+----+----------+------+ 
| 3 | C  | 1 | 
| 6 | F  | 3 | 
| 7 | G  | 6 | 
+----+----------+------+ 
3 rows in set (0.01 sec) 
相关问题