子请求MySQL

时间:2012-11-29 09:42:15

标签: mysql sql subquery

我在我试图做的子请求上被阻止了,但我看不出该怎么办。 我有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

如果有人有想法。

Kai23

2 个答案:

答案 0 :(得分:0)

您在此上下文中使用的子选择有点错误。使用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个值作为返回:idcount),所以你加入子结果它是id,应该将相应的计数作为值。

答案 1 :(得分: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