2012-11-29 75 views
1

我被一個子請求阻塞,我嘗試做,但我不知道該怎麼做。 我有4個表:子請求MySQL

  • 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 

如果有人有一個id EA。

Kai23

+1

你能提供你的表結構?你的預期結果是什麼? – SubRed

+0

你好,這裏是一個鏈接全表: http://kai23.fr/myam。SVG 而對於結果,這將是類似的東西: 'product_id', [...] 'stock_total', 'stock_vente', 'number_of_orders_in_pending_for_this_product' – Kai23

回答

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相加,並應將相應的計數作爲值。

+0

謝謝你的提示,這已經足夠做好一件! – Kai23

+0

我很高興你得到它的工作:)如果有幫助,請將該帖子標記爲解決方案。 – Tiit

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