Non-recursive unlimited classification. For more information, see

Source: Internet
Author: User
Non-recursive unlimited classification. For more information, see this post edited by vbker at 10:19:59 on.

The database fields are as follows. Currently, the region is used as the column, cid is the column ID, category is the column name, chain is the relationship tree, and rank is the column sorting.
Mysql> select * from mc_category; + ----- + ---------- + ------------ + ------ + | cid | category | chain | rank | + ----- + ---------- + -------------- + ------ + | 1 | domestic | 0_1 | 2 | 2 | Foreign | 0_2 | 1 | 3 | North China | 0_1_3 | 2 | 4 | northeast China | 0_1_4 | 1 | 5 | West China | 0_00005 | 3 | 6 | Henan | 0_1_3_6 | 1 | 7 | Zhengzhou | 0_1_3_6_7 | 2 | 8 | Gongyi | 0_1_3_6_7_8 | 1 | 9 | Luoyang | 0_1_3_6_9 | 1 | 10 | Yanshi | 0_1_3_6_9_10 | 1 | 11 | liaoning | 0_1_4 | 3 | 12 | Jilin | 0_20174_11_12 | 1 | 13 | Shangjie | 0_1_3_6_7_13 | 2 | + ----- + ---------- + -------------- + ------ +


Use
select category,chain from mc_category group by chain order by chain;

The directory tree can be listed according to the correct parent-child relationship, but now I still need to sort the columns at the same level by rank value. can this be done using mysql?




========================================================== ====================================


Reply to discussion (solution)

My post is lost so quickly !!!

Mysql syntax is not very proficient. I usually output all the data and then list the desired directory tree by assembling arrays.

Thank you. upstairs.

Select category, chain from mc_category group by chain order by chain, rank

Select category, chain from mc_category group by chain order by chain, rank

This does not work. this can be sorted by rank only when the chain is the same, but now the chain values are different, sorting by rank does not work.

How can it be called the same level? Chain is the same?

We recommend that you paste the table creation and insert statements to facilitate testing ..

Help me!

In fact, you change the data in the table

Insert into mc_category (cid, category, chain, rank) values (1, 'China', '01', 2), (2, 'foreign ', '02 ', 1), (3, 'North China ', '123', 2), (4, 'Northeast', '123', 1), (5, 'West ', '20140901', 3), (6, 'Henan ', '20160901', 1), (7, 'zhengzhou', '20160901', 2), (8, 'gongyi', '000000', 1), (9, 'luoyang ', '000000', 1), (10, 'yanshi', '000000', 1 ), (11, 'Liaoning ', '123', 3), (12, 'jilin', '123', 1), (13, 'shangjie', '123 ', 2)
In this way, they are all taken together to solve the problem.
Two characters long indicates a level

Direct

And despise baidu to prevent external links. The new image is as follows:

Before Gongyi, the reason is that rank is in descending order.
So what is the reason why Luoyang and Yanshi are in front of Zhengzhou?

From the length of the chain, we can see that the columns of the same level in Zhengzhou and Luoyang should be sorted by rank according to requirements. Therefore, Luoyang should be in front of Zhengzhou, and after the order is adjusted, the parent-child relationship of the directory tree cannot be messy. Yanshi is a subtopic of Luoyang, so Yanshi should follow Luoyang. So Luoyang and Yanshi should be in Zhengzhou before.

This post was last edited by xuzuning at 10:59:59

As I have already said, "The reason for putting the streets in front of Gongyi is rank descending order"
In descending order of rank, Zhengzhou should be in front of Luoyang
Because the rank of Luoyang is 1, the rank of Zhengzhou is 2

Different standards cannot be implemented? Think about it again?

$ N = 10;
select category,chain,rank from mc_category order by rpad(left(chain, 2),$n,'0') desc,rpad(left(chain, 4),$n,'0') asc,rpad(left(chain, 6),$n,'0') asc,length(chain)>6,rank desc
Foreign 021 domestic 012 North China 01032 Henan 0103061 Zhengzhou 010306072 Shangjie 01030607132 Gongyi 01030607081 Luoyang 010306091 Yanshi 01030609101 northeast 01041 Liaoning 0104103 Jilin 010411121 Western 01053

Now, I understand rank in descending order. what I want to talk about is order by chain and rank. In this way, the chain is first sorted based on the chain, and then the values of the chain are sorted by rank, but the chain values here are not the same, therefore, rank does not work in ascending or descending order. I will study the above query first. thank you!

To achieve this, you must first group the data and then sort the data by group. Each place name above represents a column, and chain is the relationship chain of the column.

Nagging and understanding wrong

The subject requires that sub-categories of the same parent category be sorted by rank

This can only be done in mysql. you must read and process the classification tree using a program.

Cache is recommended for this classification tree.

Why am I wrong?

Parent category 01030607
Zhengzhou 01030607 2
Shangjie 0103060713 2
Gongyi 0103060708 1

Parent category 010306
Henan 010306 1
Zhengzhou 01030607 2
Luoyang 01030609 1

Parent category 01
China 01 2
China North 0103 2
Northeast China 0104 1

The landlord implemented the dual standard during interpretation.

Non-recursive unlimited classification. For more information, see [question Points: 40 points, Post author vbker]. Is there a solution to this problem? Hope to enlighten me ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.