如何在单个表中获取所有父名称及其ID

时间:2015-05-08 11:21:14

标签: php mysql

我有下表:

enter image description here

现在我需要获取其父类别

的所有类别

喜欢:

category_id 1 -> "category_id=1;category_name=Electronic";<br/>
category_id 2 -> "category_id=2;category_name=Media";<br/>
category_id 3 -> "category_id=3;category_name=Mobile-Charger,parent_name=mobile" parent_id=1;<br/>
category_id 4 -> "category_id=4;category_name=Mobile-Charger,parent_name=Mobile" parent_id=1 ;
// and in that case Mobile also has a parent Electronic Product

我需要获取9类别的所有层次关系,如关联数组

喜欢这种类型:

  

[{ “CATEGORY_ID”: “1”, “CATEGORY_NAME”:“电子   产品”, “PARENT_ID”: “0”, “孩子”:[{ “CATEGORY_ID”: “3”, “CATEGORY_NAME”: “移动设备”, “PARENT_ID”: “1”, “子”:[{ “CATEGORY_ID” : “4”, “CATEGORY_NAME”: “移动设备充电器”, “PARENT_ID”: “3”, “子”:[]}]}]},{ “CATEGORY_ID”: “2”, “CATEGORY_NAME”:“媒体”, “PARENT_ID”: “0”, “孩子”:[{ “CATEGORY_ID”: “5”, “CATEGORY_NAME”: “媒体胎-1”, “PARENT_ID”: “2”, “子”:[] },{ “CATEGORY_ID”: “6”, “CATEGORY_NAME”: “媒体胎-2”, “PARENT_ID”: “2”, “子”:[{ “CATEGORY_ID”: “8”, “CATEGORY_NAME”:”媒体儿童-6-儿童-1" , “PARENT_ID”: “6”, “孩子”:[]},{ “CATEGORY_ID”: “9”, “CATEGORY_NAME”:“媒体胎-6-儿童-2- “ ”PARENT_ID“: ”6“, ”孩子“:[]}]},{ ”CATEGORY_ID“: ”7“, ”CATEGORY_NAME“: ”媒体胎-3“, ”PARENT_ID“: ”2“,”子“:[]}]}]

4 个答案:

答案 0 :(得分:2)

您应该使用mysql join查询来获得所需的结果......

以下查询可以使用..

使用MYSQL JOINS

select c1.category_id,c1.category_name,c2.category_name as parent_name
from category c1
left join category c2 on (c2.parent_id = c1.category_id)

使用子查询

SELECT c1.category_id,c1.category_name,
(select c2.category_name from category c2 where c2.parent_id = c1.category_id limit 1) as parent_name
from category c1

如果需要进一步的帮助,请告诉我。

您可以在此处详细了解mysql joinshttps://dev.mysql.com/doc/refman/5.0/en/join.htmlmysql subqueries此处:https://dev.mysql.com/doc/refman/5.0/en/subqueries.html

答案 1 :(得分:0)

您需要先获得基本类别(没有父级)和类别 儿童类别。

UNION他们的结果,我们得到了理想的结果。

SELECT category_id, category_name, '' as parent 
FROM `categories` 
WHERE `parent_d` = 0 
UNION 
SELECT c.category_id, c.category_name, P.category_name as parent 
FROM `categories` c 
INNER JOIN categories P ON c.`parent_d` = P.category_id 
WHERE c.`parent_d` != 0

以下是demo

答案 2 :(得分:0)

试试这个。

 SELECT * FROM category t1 JOIN other_table t2 ON t1.parent_id = t2.parent_id

答案 3 :(得分:0)

<?php

include 'config.php';
static $all_data =  array();
$all_data =  buildCategories($all_data, 0);
echo json_encode($all_data);

function buildCategories($all_data2, $parent) 
{

    $q = "Select * from category where parent_id = ".$parent;
    if($rs = getRSQuery($q))
    {
        $all_data2 = $rs;
                if($all_data2!='Not Found')
                {
        foreach($all_data2 as $index => $catDetails)
        {
            $all_data2[$index]['child'] = array();
            $all_data2[$index]['child'] =  buildCategories($all_data2[$index]['child'], $catDetails['category_id']) ;
        }
                }
    }
    else
        return array();
    return $all_data2;

}


function getRSQuery($query)
{

    $query_result = array();
    $result_set=  mysql_query($query);
    if(!$result_set)
    {
        die('Could not get data: ' . mysql_error());
    }
    $num_rows= mysql_num_rows($result_set);
    if($num_rows>0)
    {
      $index = 0;
      while(($row = mysql_fetch_array($result_set, MYSQL_ASSOC)) != FALSE)
        {
            foreach($row as $colName =>$val)
            {
                $query_result[$index][$colName] = $val;             
            }
            $index++;
        }
        return $query_result;       
    }
    else
        return "Not Found";
}



?>