English 中文(简体)
MySql Query php
原标题:MySql Query issue in php
  • 时间:2012-05-10 09:43:55
  •  标签:
  • php
  • mysql
  • sql

i have a db table category_path it is in parent child relationship and its look like this

-----------------------------
 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     | 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

如何通过数据库查询或编码做到这一点?

最佳回答

最后,你的解决办法是:

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> ;

希望能发挥作用!

问题回答

use the Graph Engine, Its what its designed for http://openquery.com/products/graph-engine

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




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签