MySql:用DESC命令获得父,子和孙子

时间:2015-04-09 04:30:49

标签: mysql

表名tbl_product_category

id  parent_id   name    ordering

1   0   Air Conditioner 1
2   0   Commercial AC   2
3   1   Refrigeration   3
4   2   Air Cooler  4
5   2   Water Dispenser 5
6   1   Rice Cooker 6
7   4   Imduction Oven  7
8   6   Fan 8

这是我的mysql查询,我试图添加ORDER BY ordering DESC, level

SELECT SQL_CALC_FOUND_ROWS men.*,
            (
                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.', LPAD( CONCAT(sub2.ordering, sub2.ordering), 10, '0'), '.', LPAD( CONCAT(sub3.ordering, sub3.ordering), 10, '0'), '.', LPAD( CONCAT(sub4.ordering, sub4.ordering), 10, '0'), '#lv5')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                INNER JOIN `tbl_product_category` sub2 ON (sub1.id = sub2.parent_id) 
                INNER JOIN `tbl_product_category` sub3 ON (sub2.id = sub3.parent_id) 
                INNER JOIN `tbl_product_category` sub4 ON (sub3.id = sub4.parent_id) 
                WHERE sub4.id = men.id AND parent.parent_id = 0

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.', LPAD( CONCAT(sub2.ordering, sub2.ordering), 10, '0'), '.', LPAD( CONCAT(sub3.ordering, sub3.ordering), 10, '0'), '#lv4')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                INNER JOIN `tbl_product_category` sub2 ON (sub1.id = sub2.parent_id) 
                INNER JOIN `tbl_product_category` sub3 ON (sub2.id = sub3.parent_id) 
                WHERE sub3.id = men.id AND parent.parent_id = 0 

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.', LPAD( CONCAT(sub2.ordering, sub2.ordering), 10, '0'), '#lv3')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                INNER JOIN `tbl_product_category` sub2 ON (sub1.id = sub2.parent_id) 
                WHERE sub2.id = men.id AND parent.parent_id = 0 

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '#lv2')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                WHERE sub1.id = men.id AND parent.parent_id = 0

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '#lv1')
                FROM `tbl_product_category` parent
                WHERE parent.id = men.id AND parent.parent_id = 0 

            ) AS level

        FROM `tbl_product_category` men
        ORDER BY level

ORDER BY level

的结果
level (new field)
0000000011#lv1
0000000011.0000000044#lv2
0000000011.0000000044.0000000077#lv3
0000000011.0000000055#lv2
0000000088#lv1
0000001111#lv1
0000001111.0000000033#lv2
0000001111.0000000066#lv2

ORDER BY ordering DESC, level

的结果
level (new field)
0000001111#lv1
0000000088#lv1
0000000011.0000000044.0000000077#lv3
0000001111.0000000066#lv2
0000000011.0000000055#lv2
0000000011.0000000044#lv2
0000001111.0000000033#lv2
0000000011#lv1

这是我希望的:

level (new field)
0000001111#lv1
0000001111.0000000066#lv2
0000001111.0000000033#lv2
0000000088#lv1
0000000011#lv1
0000000011.0000000055#lv2
0000000011.0000000044#lv2
0000000011.0000000044.0000000077#lv3

1 个答案:

答案 0 :(得分:0)

您可能需要包含一个仅用于排序的附加字段。这应该有效:

SELECT SQL_CALC_FOUND_ROWS men.*,
            (
                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.', LPAD( CONCAT(sub2.ordering, sub2.ordering), 10, '0'), '.', LPAD( CONCAT(sub3.ordering, sub3.ordering), 10, '0'), '.', LPAD( CONCAT(sub4.ordering, sub4.ordering), 10, '0'), '#lv5')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                INNER JOIN `tbl_product_category` sub2 ON (sub1.id = sub2.parent_id) 
                INNER JOIN `tbl_product_category` sub3 ON (sub2.id = sub3.parent_id) 
                INNER JOIN `tbl_product_category` sub4 ON (sub3.id = sub4.parent_id) 
                WHERE sub4.id = men.id AND parent.parent_id = 0

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.', LPAD( CONCAT(sub2.ordering, sub2.ordering), 10, '0'), '.', LPAD( CONCAT(sub3.ordering, sub3.ordering), 10, '0'), '#lv4')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                INNER JOIN `tbl_product_category` sub2 ON (sub1.id = sub2.parent_id) 
                INNER JOIN `tbl_product_category` sub3 ON (sub2.id = sub3.parent_id) 
                WHERE sub3.id = men.id AND parent.parent_id = 0 

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.', LPAD( CONCAT(sub2.ordering, sub2.ordering), 10, '0'), '#lv3')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                INNER JOIN `tbl_product_category` sub2 ON (sub1.id = sub2.parent_id) 
                WHERE sub2.id = men.id AND parent.parent_id = 0 

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '#lv2')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                WHERE sub1.id = men.id AND parent.parent_id = 0

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '#lv1')
                FROM `tbl_product_category` parent
                WHERE parent.id = men.id AND parent.parent_id = 0 

            ) AS level,
              (
                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.', LPAD( CONCAT(sub2.ordering, sub2.ordering), 10, '0'), '.', LPAD( CONCAT(sub3.ordering, sub3.ordering), 10, '0'), '.', LPAD( CONCAT(sub4.ordering, sub4.ordering), 10, '0'), '')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                INNER JOIN `tbl_product_category` sub2 ON (sub1.id = sub2.parent_id) 
                INNER JOIN `tbl_product_category` sub3 ON (sub2.id = sub3.parent_id) 
                INNER JOIN `tbl_product_category` sub4 ON (sub3.id = sub4.parent_id) 
                WHERE sub4.id = men.id AND parent.parent_id = 0

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.', LPAD( CONCAT(sub2.ordering, sub2.ordering), 10, '0'), '.', LPAD( CONCAT(sub3.ordering, sub3.ordering), 10, '0'), '.9999999999')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                INNER JOIN `tbl_product_category` sub2 ON (sub1.id = sub2.parent_id) 
                INNER JOIN `tbl_product_category` sub3 ON (sub2.id = sub3.parent_id) 
                WHERE sub3.id = men.id AND parent.parent_id = 0 

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.', LPAD( CONCAT(sub2.ordering, sub2.ordering), 10, '0'), '.9999999999.9999999999')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                INNER JOIN `tbl_product_category` sub2 ON (sub1.id = sub2.parent_id) 
                WHERE sub2.id = men.id AND parent.parent_id = 0 

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.', LPAD( CONCAT(sub1.ordering, sub1.ordering), 10, '0'), '.9999999999.9999999999.9999999999')
                FROM `tbl_product_category` parent 
                INNER JOIN `tbl_product_category` sub1 ON (parent.id = sub1.parent_id) 
                WHERE sub1.id = men.id AND parent.parent_id = 0

                UNION

                SELECT CONCAT( LPAD( CONCAT(parent.ordering, parent.ordering), 10, '0') , '.9999999999.9999999999.9999999999.9999999999')
                FROM `tbl_product_category` parent
                WHERE parent.id = men.id AND parent.parent_id = 0 

            ) AS sorting


        FROM `tbl_product_category` men
        ORDER BY sorting desc

以下是一个小例子:http://sqlfiddle.com/#!9/be613/3