Mysql
 sql >> Database >  >> RDS >> Mysql

selezionare somma qty da 2 tabelle

Sembra che tu non stia utilizzando order table diverso dal controllo se esiste, quindi puoi usare EXISTS()

SELECT p.product_id,sum(p.qty) as qty
FROM (SELECT product_id,qty,id_order FROM order_detail_1
      WHERE product_id IS NOT NULL
      UNION ALL 
      SELECT product_id,qty,id_order FROM order_detail_2
      WHERE product_id IS NOT NULL) p
WHERE EXISTS(SELECT 1 FROM order o 
             WHERE o.id = p.id_order)
GROUP BY p.product_id