English 中文(简体)
Mysql Innodb, 取取父行和子女, 以便填入 ul
原标题:Mysql Innodb, fetching parent rows and their children in order for rendering in an ul
  • 时间:2012-05-22 14:32:07
  •  标签:
  • mysql
  • sql

我拥有以下数据库结构:

注意字段 fk_ parent_ group_ id 可以是根组的 NULL, 也可以是根组的 ID 。 非 NULL 的 fk_ parent_ group_ id 组不能有子组 。

我试图提供以下数据:

16  Module 1    0   5       1
19  Submodule 1 0       16  1
20  Submodule 2 0       16  1
22  Submodule 3 0       16  1
24  Module 2    0           1
25  Module 3    0           1
26  Submodule 4 0       25  1
27  Submodule 5 0       25  1
28  Submodule 6 0       25  1
29  Module 4    0           1
30  Module 5    1           1
31  Module 6    0           1

顺便说一句,数据输入的方式是正确的,我想要它输出, 我想为 JavaScript 手风琴样式部件输出格式格式, 在一系列未排序的列表中 :

    Module 1
    • Submodule 1
    • Submodule 2
    • Submodule 3
    ...

有效来说,我想选择一个根组,然后是儿童组。

对不起,如果我把太多的信息在这里, 我尝试了加入自己,但不能让它 工作了!

最佳回答
SELECT * FROM group ORDER BY CONCAT_WS( _ , fk_parent_group_id, id)
问题回答

我不确定这是不是你想要的,请告诉我:

我用重要的字段( ID、 模块、 模块子) 创建一个表格, 我填满了一些愚蠢的数据, 这就是我所拥有的。

mysql> show create table groupsG
*************************** 1. row ***************************
       Table: groups
Create Table: CREATE TABLE `groups` (
  `id` int(11) DEFAULT NULL,
  `modu` varchar(200) DEFAULT NULL,
  `sub_group` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> insert into groups (id,modu,sub_group) values (16, Module 1 ,null),
    -> (19, Submodule 1  ,16),
    -> (20, Submodule 2  , 16),
    -> (22, Submodule 3  , 16),
    -> (24, Module 2 ,null),
    -> (25, Module 3 ,null),
    -> (26, Submodule 4 , 25),
    -> (27, Submodule 5 , 25),
    -> (28, Submodule 6 , 25);
Query OK, 9 rows affected (0.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql> select * from groups;
+------+-------------+-----------+
| id   | modu        | sub_group |
+------+-------------+-----------+
|   16 | Module 1    |      NULL |
|   19 | Submodule 1 |        16 |
|   20 | Submodule 2 |        16 |
|   22 | Submodule 3 |        16 |
|   24 | Module 2    |      NULL |
|   25 | Module 3    |      NULL |
|   26 | Submodule 4 |        25 |
|   27 | Submodule 5 |        25 |
|   28 | Submodule 6 |        25 |
+------+-------------+-----------+
9 rows in set (0.00 sec)

重要的事情是我应用了“自相残杀”的查询, 它看起来是这样的:

mysql> select * from groups as t1 join groups as t2  on t1.id=t2.sub_group;
+------+----------+-----------+------+-------------+-----------+
| id   | modu     | sub_group | id   | modu        | sub_group |
+------+----------+-----------+------+-------------+-----------+
|   16 | Module 1 |      NULL |   19 | Submodule 1 |        16 |
|   16 | Module 1 |      NULL |   20 | Submodule 2 |        16 |
|   16 | Module 1 |      NULL |   22 | Submodule 3 |        16 |
|   25 | Module 3 |      NULL |   26 | Submodule 4 |        25 |
|   25 | Module 3 |      NULL |   27 | Submodule 5 |        25 |
|   25 | Module 3 |      NULL |   28 | Submodule 6 |        25 |
+------+----------+-----------+------+-------------+-----------+
6 rows in set (0.00 sec)

如果您想要简化结果:

mysql> select t1.id as ID, t1.modu as PARENT, t2.modu as SON from groups as t1 join groups as t2  on t1.id=t2.sub_group;
+------+----------+-------------+
| ID   | PARENT   | SON         |
+------+----------+-------------+
|   16 | Module 1 | Submodule 1 |
|   16 | Module 1 | Submodule 2 |
|   16 | Module 1 | Submodule 3 |
|   25 | Module 3 | Submodule 4 |
|   25 | Module 3 | Submodule 5 |
|   25 | Module 3 | Submodule 6 |
+------+----------+-------------+
6 rows in set (0.00 sec)

它只显示有孩子的模块:

如果您想要全部显示它们, 您可以做到这一点

mysql> select t1.id as ID, t1.modu as PARENT, t2.modu as SON from groups as t1 join groups as t2  on t1.id=IFNULL(t2.sub_group,t2.id);
+------+----------+-------------+
| ID   | PARENT   | SON         |
+------+----------+-------------+
|   16 | Module 1 | Module 1    |
|   16 | Module 1 | Submodule 1 |
|   16 | Module 1 | Submodule 2 |
|   16 | Module 1 | Submodule 3 |
|   24 | Module 2 | Module 2    |
|   25 | Module 3 | Module 3    |
|   25 | Module 3 | Submodule 4 |
|   25 | Module 3 | Submodule 5 |
|   25 | Module 3 | Submodule 6 |
+------+----------+-------------+
9 rows in set (0.00 sec)

或以这种方式

mysql> select t1.id as ID, t1.modu as PARENT, if(t2.modu=t1.id,NULL,t2.sub_group) as SON from groups as t1 join groups as t2  on t1.id=IFNULL(t2.sub_group,t2.id);
+------+----------+------+
| ID   | PARENT   | SON  |
+------+----------+------+
|   16 | Module 1 | NULL |
|   16 | Module 1 |   16 |
|   16 | Module 1 |   16 |
|   16 | Module 1 |   16 |
|   24 | Module 2 | NULL |
|   25 | Module 3 | NULL |
|   25 | Module 3 |   25 |
|   25 | Module 3 |   25 |
|   25 | Module 3 |   25 |
+------+----------+------+
9 rows in set, 9 warnings (0.00 sec)




相关问题
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 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签