如何从db获取多类别和产品作为路径

时间:2016-12-21 08:47:34

标签: javascript mysql angularjs node.js database

更新:数据样本

DROP TABLE IF EXISTS `tbl_products_categories`;
DROP TABLE IF EXISTS `tbl_products2`;
DROP TABLE IF EXISTS `tbl_categories`;

CREATE TABLE `tbl_categories` (
  `catID` int(5) NOT NULL AUTO_INCREMENT,
  `fkAppID` int(5) NOT NULL,
  `parentId` int(5) DEFAULT NULL COMMENT 'η route κατηγορία έχει NULL πατρικη κατηγορία',
  `CatName` varchar(255) NOT NULL,
  `CatPosition` int(5) DEFAULT NULL,
  `CatUrl` varchar(512) DEFAULT NULL,
  `CatAvailability` int(1) DEFAULT '1',
  `CatComments` varchar(255) DEFAULT NULL,
  `CatField1` varchar(255) DEFAULT NULL,
  `CatField2` varchar(255) DEFAULT NULL,
  `CatField3` varchar(255) DEFAULT NULL,
  `CatField4` varchar(255) DEFAULT NULL,
  `CatField5` varchar(255) DEFAULT NULL,
  `CatLastUpdate` datetime DEFAULT NULL,
  `CatDeleted` varchar(1) DEFAULT '0',
  PRIMARY KEY (`catID`),
  KEY `categories_apps` (`fkAppID`)
) ENGINE=InnoDB AUTO_INCREMENT=5057 DEFAULT CHARSET=utf8;

CREATE TABLE `tbl_products2` (
  `prID` int(5) NOT NULL AUTO_INCREMENT,
  `PrName` varchar(255) NOT NULL,
  `PrPrice` decimal(5,2) DEFAULT '0.00',
  `PrSPrice` decimal(5,2) DEFAULT '0.00',
  `PrAvailability` int(1) DEFAULT '1',
  `PrUrl` varchar(512) DEFAULT NULL,
  `PrComments` text,
  `PrPosition` int(5) DEFAULT NULL,
  `PrSKU` varchar(255) DEFAULT NULL,
  `PrField2` varchar(255) DEFAULT NULL,
  `PrField3` varchar(255) DEFAULT NULL,
  `PrField4` varchar(255) DEFAULT NULL,
  `PrField5` varchar(255) DEFAULT NULL,
  `PrField6` varchar(255) DEFAULT NULL,
  `PrField7` varchar(255) DEFAULT NULL,
  `PrField8` varchar(255) DEFAULT NULL,
  `PrField9` varchar(255) DEFAULT NULL,
  `PrField10` varchar(255) DEFAULT NULL,
  `PrLastUpdate` datetime DEFAULT NULL,
  `PrDeleted` varchar(1) DEFAULT '0',
  `PrType` varchar(20) NOT NULL,
  `PrVendor` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`prID`)
) ENGINE=InnoDB AUTO_INCREMENT=10450 DEFAULT CHARSET=utf8;

CREATE TABLE `tbl_products_categories` (
  `prId` int(11) NOT NULL,
  `catId` int(11) NOT NULL,
  KEY `products_cat` (`prId`),
  KEY `categories_pr` (`catId`),
  CONSTRAINT `products_cat` FOREIGN KEY (`prId`) REFERENCES `tbl_products2` (`prID`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `categories_pr` FOREIGN KEY (`catId`) REFERENCES `tbl_categories` (`catID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `tbl_categories` VALUES ('5042', '5', null, 'Phones', '1', null, '1', 'Phones', null, null, null, null, null, null, '0');
INSERT INTO `tbl_categories` VALUES ('5044', '5', '5042', 'SmartPhones', '1', null, '1', 'SmartPhones', null, null, null, null, null, null, '0');
INSERT INTO `tbl_categories` VALUES ('5049', '5', '5042', 'Accessories', null, null, '1', 'Accessories', null, null, null, null, null, null, '0');
INSERT INTO `tbl_categories` VALUES ('5050', '5', '5042', 'iphone', null, null, '1', 'iphone', null, null, null, null, null, null, '0');
INSERT INTO `tbl_categories` VALUES ('5051', '5', '5044', 'ALCATEL', null, null, '1', null, null, null, null, null, null, null, '0');
INSERT INTO `tbl_categories` VALUES ('5052', '5', '5044', 'APPLE', null, null, '1', null, null, null, null, null, null, null, '0');
INSERT INTO `tbl_categories` VALUES ('5053', '5', '5044', 'ASUS', null, null, '1', null, null, null, null, null, null, null, '0');
INSERT INTO `tbl_categories` VALUES ('5054', '5', '5049', 'Θηκες', null, null, '1', null, null, null, null, null, null, null, '0');
INSERT INTO `tbl_categories` VALUES ('5055', '5', '5049', 'HandsFree', null, null, '1', null, null, null, null, null, null, null, '0');
INSERT INTO `tbl_categories` VALUES ('5056', '5', '5049', 'BlueTooth', null, null, '1', null, null, null, null, null, null, null, '0');


INSERT INTO `tbl_products2` VALUES ('10447', 'Product1', '0.00', '5.00', '1', null, null, null, null, null, null, null, null, null, null, null, null, null, null, '0', '', null);
INSERT INTO `tbl_products2` VALUES ('10448', 'Product2', '0.00', '5.00', '1', null, null, null, null, null, null, null, null, null, null, null, null, null, null, '0', '', null);
INSERT INTO `tbl_products2` VALUES ('10449', 'idol4', '0.00', '5.00', '1', null, null, null, null, null, null, null, null, null, null, null, null, null, null, '0', '', null);



INSERT INTO `tbl_products_categories` VALUES ('10447', '5042');
INSERT INTO `tbl_products_categories` VALUES ('10448', '5042');
INSERT INTO `tbl_products_categories` VALUES ('10449', '5051');

我有这三个表,我需要结合它们的结果:

1。 tbl_products(所有产品
  2。 tbl_categories(我使用parentId来创建子类别
  3。 tbl_product_categories(连接带有类别的产品的表

enter image description here

包含数据的表格

enter image description here

通过that page的帮助,我可以获得类别和子类别  作为完整路径,例如:Phones->NULL>NULLPhones -> Smartphones -> Alcatel

但我也是,不知何故,我必须将产品添加到结果中,我需要让它们像这个模板一样:

enter image description here

所以,我的问题是,我可以在一个SQL查询中执行此操作, 或者我应该在不同的查询中得到结果,然后将它们与客户端上的Javascript代码结合起来。

请帮帮我,谢谢。

0 个答案:

没有答案