How to SELECT and sort it by parent-children in MySQL -
i have table looks this
id parent_id name address email telephone 2 null name address email phone 3 null name2 address2 email2 phone2 4 null name3 address3 email3 phone3 5 null name4 address4 email4 phone4 6 null name6 email6 phone6 7 5 name7 email7 phone7 8 null name8 email8 phone8
and want select on select rows, if row has parent displayed after parent. in case rows this
id parent_id name address email telephone 2 null name address email phone 3 null name2 address2 email2 phone2 4 null name3 address3 email3 phone3 5 null name4 address4 email4 phone4 7 5 name7 email7 phone7 6 null name6 email6 phone6 8 null name8 email8 phone8
the row id=7 goes before 1 id=6 because parent_id of 1 id=7 5 placed directly after row.
you can using case
statement in order by
clause:
select * yourtable order case when parentid null id else parentid end, parentid
Comments
Post a Comment