简体   繁体   English

在php mysql中获取有关其父母关系的孩子的所有详细信息

[英]Get all details of child with relation of its parents in php mysql

Cat and sub category Table (cat_tbl) : 猫和子类别表(cat_tbl)

id | cat_n    | parent_id
1  |   cat    |  0
2  |   dog    |  0
3  |   tiger  |  2
4  |   lion   |  0
5  |   abc    |  0
6  |   bcd    |  3 

Now i have a product table as below (prod_tbl) : 现在我有一个如下的产品表(prod_tbl):

id  | pwght | cid  |  cpid 
10  |  1.2  |  1   |   0
11  |  2.4  |  2   |   0
12  |  3.4  |  2   |   0
13  |  4.5  |  6   |   3

and user final weight product table is below (userprod_tbl) : 用户最终重量产品表如下(userprod_tbl):

id | pwght | cid  |  cpid | prod_id ( is above prod_tbl primary id )
1  |  1.1  |  1   |   0   |  10
2  |  2.3  |  2   |   0   |  11
3  |  3.1  |  3   |   2   |  12
4  |  4.0  |  6   |   3   |  13

RESULT : ( OUTPUT WHICH I WANT ) IS comparison of prod_tbl with userprod_tbl as below : 结果:(我想要的输出)是prod_tbl与userprod_tbl的比较,如下所示:

 Prod tbl                  Userprod tbl

 cat  1.2                  cat                 1.1

 dog  2.4                  dog   --     --     2.3
 dog  3.4                  dog  tiger   --     3.1
 dog  4.5                  dog  tiger  bcd     4.0      

Hence in above result 2.4,3.4,4.5 are belong to Parent id 2 因此,在上述结果2.4、3.4、4.5中属于父代ID 2

But i am getting as below 但我得到如下

 Prod tbl                  Userprod tbl

 cat  1.2                  cat                 1.1

 dog  2.4                  dog   --     --     2.3
 dog  3.4                  dog  tiger   --     3.1   

in above result i am not getting 4.5 value ,as 4.5 has 6,3 relation from above prod table but its parent of id 2 在上面的结果中我没有得到4.5的值 ,因为4.5从上面的prod表中有6,3个关系,但是它的ID为2的父级

Below is my query which i have return: 以下是我返回的查询:

SELECT pt.pwght , upt.pwght ,ct.cat_n,uct.cat_n,umct.cat_n
FROM prod_tbl AS pt
LEFT JOIN userprod_tbl AS upt ON (pt.id = upt.prod_id)
LEFT JOIN cat_tbl AS ct ON pt.packet_id = ct.id
LEFT JOIN cat_tbl AS uct ON upt.packet_id = uct.id
LEFT JOIN cat_tbl AS umct ON upt.parent_packet_id = umct.id

Please let me know what is missing Thanks 请让我知道缺少什么谢谢

This will solve your problem I think. 我认为这将解决您的问题。

SELECT pt.pwght AS prod_pwght, upt.pwght AS userprod_pwght ,
       ct.cat_n AS prod_catName,uct.cat_n AS Userprod_catName
FROM prod_tbl AS pt
LEFT JOIN userprod_tbl AS upt ON pt.id = upt.prod_id
LEFT JOIN cat_tbl AS ct ON pt.cid = ct.id
LEFT JOIN cat_tbl AS uct ON upt.cid = uct.id;

here is working fiddle for you - http://sqlfiddle.com/#!9/9568a/1 这是为您工作的小提琴-http://sqlfiddle.com/#!9/ 9568a/1

You cannot translate recursion into SQL. 您不能将递归转换为SQL。 You could make your query increasingly complex to cover this recursion up to a determined number of levels, but not to an undetermined number, and doing that is very slow and completely unecessary. 您可以使查询变得越来越复杂,以覆盖此递归直至确定的级别数,而不是不确定的数量,并且这样做非常缓慢且完全没有必要。

I suggest you fetch cat_tbl to your script and compute this category ladder localy instead of imposing that on MySQL. 我建议您将cat_tbl提取到脚本中并计算该类别梯形图的本地化,而不是将其强加于MySQL。

Example (Please note I'm just writing this without testing anything): 示例(请注意,我只是在编写此代码而不进行任何测试):

$cat_tree = [];

$res = $mysql->query('SELECT id, cat_n, parent_id FROM cat_tbl');
while ($row = $res->fetch_row())
    $cat_tree[$row[0]] = [$row[1], $row[2]];

function get_tree($cid)
{
    global $cat_tree;

    $result = [$cat_tree[$cid][1]];
    while ($cat_tree[$cid][2])
    {
        $cid = $cat_tree[$cid][2];
        $result[] = $cat_tree[$cid][1];
    }
    return array_reverse($result);
}

$res = $mysql->query('SELECT pt.id, pt.cid, pt.pwght, ut.cid, ut.pwght FROM prod_tbl pt INNER JOIN userprod_tbl ut ON pt.id = u.prod_id');
while ($row = $res->fetch_row())
{
    $ptree = get_tree($row[1]);
    $utree = get_tree($row[3]);
    printf("%s | %f | %s | %s\n", $ptree[0], $row[2], implode(', ', $utree), $row[4]);
}

This should output something like: 这应该输出类似:

cat | 1.2 | cat | 1.1
dog | 2.4 | dog | 2.3
dog | 3.4 | dog, tiger | 3.1
dog | 4.5 | dog, tiger, bcd | 4.0

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

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