简体   繁体   English

PHP中的MySQL查询问题

[英]MySql Query issue in php

i have a db table category_path it is in parent child relationship and its look like this 我有一个数据库表category_path,它在父子关系中,看起来像这样

-----------------------------
 id   |    parent_id
------------------------------
  1   |   NULL        
  2   |   1        
  3   |   2        
  4   |   1        
  5   |   3        
  6   |   2        

using this table i want to create a new table which will give me output like this. 使用此表,我想创建一个新表,该表将给我这样的输出。 Where the table below shows distance for each id from parent 0 to that id by traversing through its parent. 下表显示了遍历其父代的每个ID到父代0到该ID的距离。

----------------------------------
   #  |id     | parent_id | distance    
----------------------------------
   1  |  1    |   1       |   0  
   2  |  1    |   2       |   1  
   3  |  1    |   3       |   2  
   4  |  1    |   4       |   1  
   5  |  1    |   5       |   3  
   6  |  1    |   6       |   2
   7  |  2    |   2       |   0
   8  |  2    |   3       |   1
   9  |  2    |   5       |   2
   10 |  2    |   6       |   1
   11 |  3    |   3       |   0
   12 |  3    |   5       |   1
   13 |  4    |   4       |   0
   14 |  5    |   5       |   0
   15 |  6    |   6       |   0

How to get this either by database query or by coding? 如何通过数据库查询或编码来获取?

Finally Spending whole evening here is your solution: 最后,整个晚上都花在这里是您的解决方案:

function findValue($key,$src){

    return $src[$key];
}    

function inPatentList($val, $patent_list){

    return (in_array($val, $patent_list)) ? true : false;
}

function findFullTraverse($id, $src,&$str){
    if(0 != ($value = findValue($id, $src))){
        if($str==''){
            $str .= $value;
        }else{
            $str .= '_'.$value;
        }
        findFullTraverse($value,$src,$str);
    }
}
$id_parent = array(
    '1' => '0',
    '2' => '1',
    '3' => '2',
    '4' => '1',
    '5' => '3',
    '6' => '2',
);
$parent = array_values($id_parent);
$ids = array_keys($id_parent);

$depth = array();
$keys_for_value = array();
$id_parent = array_reverse($id_parent, true);
foreach($id_parent as $key => $val){

    $depth[] = $key.'_'.$key.'_0';
    if(inPatentList($key, $parent)){
        $keys_for_value = array_keys($id_parent, $key);
        $depth_found[$key] = $keys_for_value;
        foreach ($depth_found[$key] as $value){
            $str = '';
            findFullTraverse($value, $id_parent,$str);
            //echo $value.'=>'.$str.'<br/>';
            $traverse_array = explode('_', $str);
            for($i=0;$i<sizeof($traverse_array);$i++){
                $has_depth = $i + 1;
                $depth[]=$traverse_array[$i].'_'.$value.'_'.$has_depth;
            }
        }
    }
}

sort($depth);
echo '<pre>';
print_r($depth);
echo '<pre>';

Hope this should work!!! 希望这应该工作!!!

使用Graph Engine,它是为http://openquery.com/products/graph-engine设计的

SELECT `id`, `parent_id`, (`id` - `parent_id`) as `difference` 
  from `category_path`...

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

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