Mysql query -> get all data using parentID

0

Please login or register to vote for this query.

(click on this box to dismiss)

Poker Meta

Q&A about the site for serious players and enthusiasts of poker

declare @Reviews table (id int, user_id int, parent_id int, title varchar(50))

insert @Reviews values
 (101, 52, 0, 'first review'),
 (102, 52, 0, 'another review'), 
 (103, 52, 102, '1 reply review'), 
 (104, 52, 0, 'just a review'), 
 (105, 52, 0, 'cool review'), 
 (106, 52, 101, 'Another reply review');

    select  *
    from    @Reviews feat
    order by
            case 
            when parent_id = 0 
            then title 
            else    (
                    select  title 
                    from    @Reviews parent 
                    where   parent.id = feat.parent_id
                    ) 
            end
    ,       case when parent_id = 0 then 1 end desc
    ,       title

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results