1

我尝试执行的子请求被阻止,但我不知道该怎么做。我有 4 张桌子:

  • jos_vm_products => 存储的所有产品
  • jos_vm_order_items => 订单中的项目
  • jos_vm_order => 一个订单
  • stock_web_brut => 我的产品库存

我想获取特定产品的所有信息:

  • 库存数量(完成)
  • jos_vm_products 表中的产品信息(完成)
  • 特定产品的“待处理”订单数量(已冻结)

这是我的第一个请求:

SELECT  `product_id` ,  `product_weight` ,  `cdate` ,  `mdate` ,  `product_name` ,  `product_sales` ,  `product_sku` ,  `product_in_stock` , SUM( stock_bloque ) AS  'stock_bloque', SUM( stock_total ) AS  'stock_total', SUM( stock_vente ) AS  'stock_vente', ref
FROM jos_vm_product t1
INNER JOIN  `stock_web_brut` t2 ON t1.product_sku LIKE CONCAT( t2.ref,  '%' ) 
GROUP BY product_id;

我想我应该做一个这样的子请求:

SELECT 

     (
        SELECT  COUNT(t5.product_id) 
        FROM jos_vm_product t5 
        INNER JOIN jos_vm_order_item t6 ON  t5.product_id = t6.product_id 
        WHERE order_status = 'P' AND t5.product_id = 12
     ) AS "nombre_produit_pending",  
    `product_id` ,  
    `product_weight` ,  
    `cdate` ,  `mdate` ,  
    `product_name` ,  
    `product_sales` ,  
    `product_sku` ,  
    `product_in_stock` , 
    SUM( stock_bloque ) AS  'stock_bloque', 
    SUM( stock_total ) AS  'stock_total', 
    SUM( stock_vente ) AS  'stock_vente', 
    ref
FROM jos_vm_product t1
INNER JOIN  `stock_web_brut` t2 ON t1.product_sku LIKE CONCAT( t2.ref,  '%' ) 
GROUP BY product_id

而不是« 12 »,我想我应该做一些类似的事情:

WHERE order_status = 'P' AND t5.product_id = t1.product_id

但我有一个循环

以下是表格: http: //kai23.fr/myam.svg (我无法发布图片)

对于结果,它会是这样的:

`product_id` | [...] | 'number_of_orders_in_pending_for_this_product' |
    12       | [...] |               15
    13       | [...] |               5
    14       | [...] |               3

如果有人有想法。

开23

4

2 回答 2

0

在这种情况下,您使用的子选择有点错误。使用 sub-select in left outer join,如下所示:

SELECT 
    `nombre_produit_pending`.`count`
    `product_id` ,  
    `product_weight` ,  
    `cdate` ,  `mdate` ,  
    `product_name` ,  
    `product_sales` ,  
    `product_sku` ,  
    `product_in_stock` , 
    SUM( stock_bloque ) AS  'stock_bloque', 
    SUM( stock_total ) AS  'stock_total', 
    SUM( stock_vente ) AS  'stock_vente', 
    ref
FROM jos_vm_product t1
INNER JOIN  `stock_web_brut` t2 ON t1.product_sku LIKE CONCAT( t2.ref,  '%' ) 
LEFT OUTER JOIN (select count(t5.product_id) as count, t5.product_id as pid FROM jos_vm_product t5 
        INNER JOIN jos_vm_order_item t6 ON t5.product_id = t6.product_id 
        WHERE order_status = 'P'
     ) AS nombre_produit_pending ON nombre_produit_pending.pid = t1.product_id    
GROUP BY product_id

PS!我没有检查您的数据库结构和查询,我只是按照不同的顺序复制了您自己的查询,所以如果您从 mysql 收到任何错误,我可以帮助您解决它们:)

关键是子选择应该查询所有挂单并按 ID 对它们进行分组(给出 2 个值作为 return:idcount),因此您使用它的 id 加入子结果,并且应该将相应的计数作为一个值。

于 2012-11-29T14:53:57.113 回答
0

感谢 tiit,这是最后的请求:

SELECT 
`nombre_produit_pending`.`count`,
`product_id` ,  
`product_weight` ,  
`cdate` ,  `mdate` ,  
`product_name` ,  
`product_sales` ,  
`product_sku` ,  
`product_in_stock` , 
SUM( stock_bloque ) AS  'stock_bloque', 
SUM( stock_total ) AS  'stock_total', 
SUM( stock_vente ) AS  'stock_vente', 
ref
FROM jos_vm_product t1
INNER JOIN  `stock_web_brut` t2 ON t1.product_sku LIKE CONCAT( t2.ref,  '%' ) 
LEFT OUTER JOIN (SELECT COUNT(t5.product_id) AS count, t5.product_id AS pid FROM         jos_vm_product t5 
    INNER JOIN jos_vm_order_item t6 ON t5.product_id = t6.product_id 
    WHERE order_status = 'P'
    GROUP BY t5.product_id
 ) AS nombre_produit_pending ON nombre_produit_pending.pid = t1.product_id    
GROUP BY product_id
于 2012-11-29T17:42:17.543 回答