dongyi4170 2017-03-24 10:08
浏览 57

获取特定产品的所有父类别

I cant figure out the right query and all the existing answers are related to Wordpress and I'm not on wordpress, I'm building it from scratch. I've got this table

categories table

I want to get all parent ID's. If product is in category "Yellow", i want to get all the parent IDs from Yellow category, so from query "Yellow" would be 70, 68, 66, 0.

So Im spinning around with all possible joins and best result is just current category (or all of them..)

     $sql2 = "SELECT c1.ID, c1.si, c2.si, c2.ID as parent_id 
              FROM v_menu c1 
              LEFT JOIN v_menu c2 
              on (c2.parent = c1.ID) 
              WHERE (c1.ID = :c1)";
      $st2 = $this->db->prepare($sql2);
      $st2->execute(array(':c1' => $row['MID'])); // $row['MID'] => product category
      $res2 = $st2->fetchAll();

This returns only category which matches product category.

Any hint, link, solution... would be in much help.

  • 写回答

1条回答 默认 最新

  • duanpo1498 2017-03-24 10:54
    关注

    This is the correct query if your parent ID in smaller than Child ID, which usually is.

    SELECT t.ID, t.si, @pv := t.parent parent
              from (select * from v_menu order by ID desc) t
              join (select @pv := 70) tmp
              where t.ID = @pv
    

    Reference: https://stackoverflow.com/a/24901882/2269385

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题