【问题标题】:how to get all parent name and its id in a single table如何在单个表中获取所有父名称及其 id
【发布时间】:2015-05-08 11:21:13
【问题描述】:

我有下表:

现在我需要获取所有类别及其父类别

喜欢:

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","child":[{"category_id":"3","category_name":"Mobile","parent_id":"1","child":[{"category_id" :"4","category_name":"Mobile-Charger","parent_id":"3","child":[]}]}]},{"category_id":"2","category_name":"媒体","parent_id":"0","child":[{"category_id":"5","category_name":"media-child-1","parent_id":"2","child":[] },{"category_id":"6","category_name":"media-child-2","parent_id":"2","child":[{"category_id":"8","category_name":" media-child-6-child-1","parent_id":"6","child":[]},{"category_id":"9","category_name":"media-child-6-child-2 ","parent_id":"6","child":[]}]},{"category_id":"7","category_name":"media-child-3","parent_id":"2","孩子”:[]}]}]

【问题讨论】:

  • 到目前为止你尝试过什么?

标签: php mysql


【解决方案1】:

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

可以使用下面的查询..

使用 MYSQL 连接

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 joins 的更多信息:https://dev.mysql.com/doc/refman/5.0/en/join.htmlmysql subqueries 这里:https://dev.mysql.com/doc/refman/5.0/en/subqueries.html

【讨论】:

    【解决方案2】:

    您需要首先获取基本类别(没有父母)和类别 子类别。

    联合他们的结果,我们得到想要的结果。

    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

    【讨论】:

    • 如果你的第二个查询足以得到结果,为什么要使用联合,(经过一些小的修改,即左连接和删除 where 条件)
    • @NishantSolanki,你是对的。这是另一种方法。可能有效与否:)
    • 好的,有一点需要注意all the queries used in union should be fetching same number of columns in same order..
    【解决方案3】:
    <?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";
    }
    
    
    
    ?>
    

    【讨论】:

      【解决方案4】:

      试试这个。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-23
        • 2021-12-09
        • 2011-10-04
        • 2020-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多