MySQL MLM下线选择查询第一个用户

时间:2019-04-01 14:19:54

标签: mysql rdbms

我正在运行选择查询以获取下线成员。它对除第一个用户以外的所有用户都适用。

这是我的SQL摆弄着数据http://sqlfiddle.com/#!9/036c5e/2

我想获得user_id 1的下线成员

我正在像这样运行查询

SELECT 
            t1.user_id AS lev0, 
            t2.user_id as lev1, 
            t3.user_id as lev2, 
            t4.user_id as lev3,
            t5.user_id as lev4,
            t6.user_id as lev5,
            t7.user_id as lev6,
            t8.user_id as lev7,
            t9.user_id as lev8

        FROM members AS t1

            LEFT JOIN members AS t2 ON t2.boss = t1.user_id
            LEFT JOIN members AS t3 ON t3.boss = t2.user_id
            LEFT JOIN members AS t4 ON t4.boss = t3.user_id
            LEFT JOIN members AS t5 ON t5.boss = t4.user_id
            LEFT JOIN members AS t6 ON t6.boss = t5.user_id
            LEFT JOIN members AS t7 ON t7.boss = t6.user_id
            LEFT JOIN members AS t8 ON t8.boss = t7.user_id
            LEFT JOIN members AS t9 ON t9.boss = t8.user_id
            WHERE t1.user_id = 1
            order by t1.created_at

如果我替换t1.user_id =2或其他任何东西,效果很好。有人请帮忙。谢谢。

编辑 我想获取老板1的user_id。老板1是启动程序。所以我想让他的所有下线成员。我怎样才能做到这一点?非常感谢。

1 个答案:

答案 0 :(得分:1)

更改

WHERE t1.user_id = 1

收件人

WHERE t1.boss = 1