单个MySQL查询父子表值排序

时间:2014-11-06 06:58:32

标签: mysql

我面临的问题是显示特定的父子女孩价值,并且在该孩子的下面,所有子女孩的价值都是关系。实际上我创建了单个表" tree"和三列id,main(作为父)和sub(作为子)。我创建了一个树关系船,但我不知道如何获取特定的父子,他的孩子和他的孩子的树值。

enter image description here

我想向他显示a4父母他的孩子关系船的意思(父母a4孩子b3,b4,b5,b3有c1和c2,b4有c3和c4和c3有d1和d2这样表格值缩短了出)。有什么方法可以使用任何mysql查询来缩短表格??? 如果你对此有所帮助。我会很高兴的。谢谢Souvik。

3 个答案:

答案 0 :(得分:0)

如果我理解得很好,你想做一个递归连接。

使用列main和sub进行连接,可能是3个连接。

我现在没有DB进行测试,抱歉。

答案 1 :(得分:0)

尝试以下:

select * from TreeTable a where a.main = 'a4' Union select * from TreeTable  b where b.main in (select sub from TreeTable c where c.main = 'a4')

1)在脚本

下面加入“C3”
select * from TreeTable a where a.main = 'a4' and a.main = 'c3'  Union select * from TreeTable  b where b.main in (select sub from TreeTable c where c.main = 'a4' and c.main = 'c3' )

2)对于所有孩子:

 select * from TreeTable 

答案 2 :(得分:0)

我在真正的数据库上检查了这个并且它工作了 - 无论树有多大,程序都应该为你提供所有分支 - 我希望:)

DELIMITER $$

CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `display_tree`()
BEGIN

SET @_query_text = "";

DROP TEMPORARY TABLE IF EXISTS result_tree;
DROP TEMPORARY TABLE IF EXISTS check_tbl;
DROP TEMPORARY TABLE IF EXISTS iter_tree;

CREATE TEMPORARY TABLE result_tree 
SELECT DISTINCT 
    tree.main as name_level0,
    tree.sub as name_level1 
FROM tree
    LEFT OUTER JOIN tree as level1_tree
        ON tree.sub = level1_tree.main
WHERE 
    tree.main NOT IN (SELECT check_tree.sub FROM  tree as check_tree);

CREATE TEMPORARY TABLE check_tbl 
SELECT 
    result_tree.*
FROM 
    result_tree
    LEFT OUTER JOIN tree
        ON tree.main = result_tree.name_level1
WHERE NOT(ISNULL(tree.main));


SET @current_level = 1;

WHILE EXISTS(SELECT * FROM check_tbl) DO

    DROP TEMPORARY TABLE IF EXISTS iter_tree;

    CREATE TEMPORARY TABLE iter_tree 
    SELECT * FROM result_tree;

    DROP TEMPORARY TABLE result_tree;

    SET @last_level = @current_level;
    SET @current_level = @current_level + 1;

    SET @_query_text = CONCAT('CREATE TEMPORARY TABLE result_tree 
                                SELECT 
                                    iter_tree.*, 
                                    tree.sub AS name_level', @current_level, '
                                FROM 
                                    iter_tree
                                    LEFT OUTER JOIN tree
                                        ON tree.main = iter_tree.name_level', @last_level);

-- SELECT @_query_text;
    PREPARE _query FROM @_query_text;

    EXECUTE _query;

    DEALLOCATE PREPARE _query;

    DROP TEMPORARY TABLE check_tbl;

    SET @_check_query_text = CONCAT('CREATE TEMPORARY TABLE check_tbl 
                                SELECT 
                                    result_tree.*, 
                                    tree.sub AS name_level', (@current_level + 1), '
                                FROM 
                                    result_tree
                                    LEFT OUTER JOIN tree
                                        ON tree.main = result_tree.name_level', @current_level, ' 
                                WHERE NOT(ISNULL(tree.main))');

    PREPARE _check_query FROM @_check_query_text;

    EXECUTE _check_query;

    DEALLOCATE PREPARE _check_query;
END WHILE;

SELECT * FROM result_tree;

END

您可以使用以下方式运行它:

CALL display_tree();

我希望它可以帮到你。