简体   繁体   English

需要查询mysql中的多级营销

[英]Need a query for Multi-level marketing in mysql

Hi I need a query in MySQL for my PHP page, I give my table design with data: 嗨,我需要MySQL中的查询我的PHP页面,我给我的表设计数据:

tablename : tcustomers tablename:tcustomers

Name, LeftID, RightID, Code
---------------------------
Arul    102      103     101
James   104      105     102
Alex    106      107     103
David   108      109     104
Sasi    110      111     105
Prem    112      113     106
Kumar   114      115     107

what I need is when I pass arul code in data ie 101, I need to get the whole left and right of him example output as 我需要的是当我在数据中传递arul代码即101时,我需要将他的左右两侧示例输出为

LEFT         Right
James        Alex
David        Prem
Sasi         Kumar

if someone here can help me out without using storedprocdure, it will be very helpful and thanks in advance :) 如果有人在这里可以帮助我而不使用storedprocdure,那将非常有帮助,并提前感谢:)

This is a partial solution if you want only a finite number of answers for right and left elements . 如果您只需要有限数量的左右元素答案,这是一个部分解决方案

Here is a possibility with 3 right and 3 left answers with the condition Code = 101 (Arul) : 这里有3个右边和3个左边答案的可能性条件Code = 101(Arul):

SELECT
    c.Name as NameRequested,
    l1.Name as NameLeft1,
    l2.Name as NameLeft2,
    l3.Name as NameLeft3,
    r1.Name as NameRight1,
    r2.Name as NameRight2,
    r3.Name as NameRight3
FROM tcustomers c
LEFT JOIN tcustomers l1 ON c.LeftID = l1.Code
LEFT JOIN tcustomers l2 ON l1.LeftID = l2.Code
LEFT JOIN tcustomers l3 ON l2.LeftID = l3.Code
LEFT JOIN tcustomers r1 ON c.RightID = r1.Code
LEFT JOIN tcustomers r2 ON r1.RightID = r2.Code
LEFT JOIN tcustomers r3 ON r2.RightID = r3.Code
WHERE
    l.Code = 101;

Of course you can extend the number of answers you need easily . 当然, 您可以轻松扩展您需要的答案数量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM