私は、クエリを作った:合計は、MySQL
SELECT ad.spec_id, ad.height_id, ad.size_id, (SUM(ad.qty)+SUM(md.qty)) as qty, SUM(ad.qty) as arrival_qty, SUM(md.qty) as movement_qty, a.warehouse_id
FROM arrival_data ad, movement_data md, arrivals a, movements m
WHERE ad.size_id= md.size_id
AND ad.height_id = md.height_id
AND ad.spec_id = md.spec_id
AND ad.arrival_id = a.arrival_id
AND m.receiver = a.warehouse_id
AND md.movement_id = m.movement_id
GROUP BY ad.spec_id, ad.size_id, ad.height_id, m.receiver
もちろん、それは第一及び第二のテーブルが同じspec_id、size_id、height_idを持っている場合にのみ結果を示しています。私はこの問題を解決することができます読んでいる。私は長いと低速の方法で自分のタスクを解決し
右のクエリで助けてください
$arrival_qty = $this->db->getOne('
SELECT SUM(ad.qty)
FROM arrival_data ad, arrivals a
WHERE ad.arrival_id = a.arrival_id AND ad.spec_id =?i AND ad.size_id =?i AND ad.height_id =?i AND a.warehouse_id = ?i',
$product['spec_id'], $product['size_id'], $product['height_id'], $product['warehouse_id']);
$move_in_qty = $this->db->getOne('
SELECT SUM(md.qty)
FROM movement_data md, movements m
WHERE md.movement_id = m.movement_id AND md.spec_id =?i AND md.size_id =?i AND md.height_id =?i AND m.receiver =?i',
$product['spec_id'], $product['size_id'], $product['height_id'], $product['warehouse_id']);
$move_out_qty = $this->db->getOne('
SELECT SUM(md.qty)
FROM movement_data md, movements m
WHERE md.movement_id = m.movement_id AND md.spec_id =?i AND md.size_id =?i AND md.height_id =?i AND m.sender =?i',
$product['spec_id'], $product['size_id'], $product['height_id'], $product['warehouse_id']);
$invoices_qty = $this->db->getOne('
SELECT SUM(i.qty)
FROM invoice_data i, invoices m
WHERE i.invoice_id = m.invoice_id AND i.spec_id =?i AND i.size_id =?i AND i.height_id =?i AND m.warehouse_id =?i',
$product['spec_id'], $product['size_id'], $product['height_id'], $product['warehouse_id']);
$arrival_qty = (!empty($arrival_qty)) ? $arrival_qty : '0';
$products[$key]['qty'] = $arrival_qty + $move_in_qty - $move_out_qty - $invoices_qty;
多分誰かが私に1つのクエリによって
クエリで何を実行しますか?また、明示的な 'JOIN'構文を学んでください。これは、SQLクエリを書くための適切な方法です。 –
異なるテーブルからの2つの行の合計の合計を受け取りたい – Durrasell