0

has anyone have an idea of ​​the most efficient way to turn this into a while loop in Mysql?.

select * from 1eg_usuarios where 
        user_id_1eg= (select @ult_anf:= id_anfitrion_1eg from 1eg_usuarios where user_id_1eg=121) #40
        or
        user_id_1eg= (select @ult_anf:= id_anfitrion_1eg from 1eg_usuarios where user_id_1eg=@ult_anf) #13
        or
        user_id_1eg= (select @ult_anf:= id_anfitrion_1eg from 1eg_usuarios where user_id_1eg=@ult_anf) #4
        or
        user_id_1eg= (select @ult_anf:= id_anfitrion_1eg from 1eg_usuarios where user_id_1eg=@ult_anf); #1

Thanks.

1 Answer 1

1

Are you trying to query id_anfitron_1eg recursively?

MySQL 8.0 introduced the with clause

 with recursive cte (user_id_1eg, name, id_anfitrion_leg) as (
  select     user_id_1eg, name, id_anfitrion_leg
  from       1eg_usarios
  where      id_anfitrion_leg = 121
  union all
  select     p.user_id_1eg, p.name, p.id_anfitrion_leg

  from       1eg_usarios p
  inner join cte
          on p.id_anfitron_1eg = cte.user_id_1eg
)
select * from cte;

How to create a MySQL hierarchical recursive query

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.