【问题标题】:how to get products list by category name in nested set如何在嵌套集中按类别名称获取产品列表
【发布时间】:2015-08-10 03:27:10
【问题描述】:

我有以下嵌套集的 sql,我需要显示电子类别下的所有产品及其子类别

电子产品 -> 便携式电子产品 -> mp3 播放器 -> 闪存

在 mysql 中执行此操作的 sql 查询应该是什么

--
-- Table structure for table `categories`
--

CREATE TABLE IF NOT EXISTS `categories` (
  `category_id` int(11) NOT NULL,
  `name` varchar(20) NOT NULL,
  `left_node` int(11) NOT NULL,
  `right_node` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `categories`
--

INSERT INTO `categories` (`category_id`, `name`, `left_node`, `right_node`) VALUES
(1, 'electronics', 1, 20),
(2, 'televisions', 2, 9),
(3, 'tube', 3, 4),
(4, 'lcd', 5, 6),
(5, 'plasma', 7, 8),
(6, 'portable electronics', 10, 19),
(7, 'mp3 players', 11, 14),
(8, 'flash', 12, 13),
(9, 'cd players', 15, 16),
(10, '2 way radios', 17, 18);

-- --------------------------------------------------------

--
-- Table structure for table `products`
--

CREATE TABLE IF NOT EXISTS `products` (
  `product_id` int(11) NOT NULL,
  `name` varchar(40) DEFAULT NULL,
  `cat_id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`product_id`, `name`, `cat_id`) VALUES
(1, '20" TV', 3),
(2, '36" TV', 3),
(3, 'Super-LCD 42"', 4),
(4, 'Ultra-Plasma 62"', 5),
(5, 'Value Plasma 38"', 5),
(6, 'Power-MP3 5gb', 7),
(7, 'Ipod 4gb', 8),
(8, 'Porta CD', 9),
(9, 'Walkman', 9),
(10, 'Family Talk 360', 10);

-- --------------------------------------------------------

--
-- Table structure for table `product_categories`
--

CREATE TABLE IF NOT EXISTS `product_categories` (
  `product_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `product_categories`
--

INSERT INTO `product_categories` (`product_id`, `category_id`) VALUES
(1, 3),
(2, 3),
(3, 4),
(4, 5),
(5, 5),
(6, 7),
(7, 8),
(8, 9),
(9, 9),
(10, 10);

【问题讨论】:

    标签: php mysql nested set


    【解决方案1】:
    SELECT c.name, c.category_id, c.left_node, c.right_node, p.product_id, p.name
    from categories c, products p
    where c.category_id = p.category_id group by c.category_id
    

    【讨论】:

    • 如果引入了新表,那么如何达到相同的结果?如果不存在则创建表product_categories (product_id int(11) NOT NULL, category_id int(11) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;插入product_categories (product_id, category_id) 值 (1, 3), (2, 3), (3, 4), (4, 5), (5, 5), (6, 7) , (7, 8), (8, 9), (9, 9), (10, 10);
    • Sidhu 如果你不介意的话,你能更清楚地解释我吗...!!! sidhu 我有一个建议给你。即通过外键关系。
    • 我已经更新了表结构,SELECT p.* FROM products as p LEFT JOIN product_categories AS pc ON (pc.product_id = p.product_id) WHERE pc.category_id IN (SELECT node.category_id FROM categories as node, categories as parent WHERE node.left_node BETWEEN parent.left_node AND parent.right_node AND parent.category_id = 6 ORDER BY node.left_node)
    • 上面的查询工作正常,它使用了IN子句,我想用JOINS替换IN子句怎么做?
    【解决方案2】:

    SELECT p.* FROM product p,categories c WHERE p.category_id = c.category_id and p.category_id = 1

    【讨论】:

      【解决方案3】:

      您可以使用INNER JOIN 来获取您想要的行。

      SELECT categories.name, products.name
      FROM categories
      INNER JOIN products ON categories.category_id = products.category_id
      WHERE categories.category_id = ?
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-29
        • 1970-01-01
        • 2013-05-28
        • 2012-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多