我有一个数据“产品”表和第二个表“product_components”。Mysql/PHP无限子查询
“产品”由任意数量的“product_components”组成,每个“product_components”本身也可以有任意数量的“product_components”。产品和组件使用第三个表“product_bom”链接。
成本存储在“product_components”级别。
我可以轻松地通过合并每个“产品”行来获得组件的价格,并且我可以在其内部重复查询以查找第二层组件。
任何人都可以建议我可以无限检查每个组件的每个组件的另一个级别的组件...........等。
感谢,
Euphbasio
更新: 这是我顶层基本查询。我可以在最终的循环中嵌套一个这样的副本,然后再次,这意味着有限的可能性。
SELECT `product_bom`.*, `products_boml`.`product_code` AS `pcode`,`products_boml`.`title` AS `title`, IF(`currency_rates`.`rate`>0,`products_boml`.`cost` * `currency_rates`.`rate`,`products_boml`.`cost`) AS `pcost`,`currency`.`currency` AS `curletters`FROM `product_bom` LEFT JOIN `products` `products_boml` ON `products_boml`.`id`=`product_bom`.`bom_part_id`
LEFT JOIN `products` `products_tlp` ON `products_tlp`.`id`='$product_id'
LEFT JOIN `business_relations` `br_boml_vendor` ON `br_boml_vendor`.`id`=`products_boml`.`vendor_id`
LEFT JOIN `business_relations` `br_tlp_vendor` ON `br_tlp_vendor`.`id`=`products_tlp`.`vendor_id`
LEFT JOIN `currency_rates` ON `currency_rates`.`base_currency_id`=`br_boml_vendor`.`currency` AND `currency_rates`.`exchange_currency_id`=`br_tlp_vendor`.`currency`
LEFT JOIN `currency` ON `currency`.`id`=`br_tlp_vendor`.`currency`
WHERE `product_bom`.`product_id`='$product_id'
ORDER BY `products_boml`.`product_code`
我正在转换货币也,但可以忽略。
谢谢。
添加代码片段 – Rasel
mysql无法顺利执行无限递归,但是您可以使用轻微不同的数据模型实现类似的结果http://stackoverflow.com/questions/3704130/recursive-mysql-query或通过做一些像存储过程http://guilhembichot.blogspot.co.uk/2013/11/with-recursive-and-mysql.html(但是你必须在每个深度级别手动编写SP路由) – Dave
谢谢,我会去看看这些。似乎没有太多的选择...... – euphbasio