简体   繁体   中英

mysql query from three tables

I'm using this query

SELECT
orders_list.nr_comanda_orders AS Comanda,
SUM(orders_list.cantitate) AS Total, 
SUM(orders_list.qty_to_add) AS InStoc,
COUNT(entries_list.id) AS Picked
FROM orders_list 
LEFT JOIN entries_list ON (entries_list.file_id = orders_list.id AND entries_list.orders_list_id = orders_list.nr_comanda_orders)
GROUP BY orders_list.nr_comanda_orders
ORDER BY orders_list.nr_comanda_orders DESC

which has this result

Comanda    | Total | InStoc | Picked
-----------|----------------|--------
 AAY280838 |   64  | 6      |   0
 AAY280837 |   50  | 8      |   0
 AAY280836 |   89  | 8      |   0

and a second query

SELECT
entries_list.orders_list_id AS Comanda,
SUM(orders_list.cantitate) AS Total, 
SUM(orders_list.qty_to_add) AS InStoc,
COUNT(entries_list.id) AS Picked
FROM  entries_list 
LEFT JOIN orders_list ON (entries_list.file_id = orders_list.id AND entries_list.orders_list_id = orders_list.nr_comanda_orders) 
LEFT JOIN orders_uploaded ON orders_list.file_id = orders_uploaded.id
GROUP BY 
entries_list.orders_list_id  
ORDER BY 
entries_list.orders_list_id DESC,
orders_uploaded.id_routing DESC, 
orders_uploaded.upload_date DESC, 
orders_uploaded.progress ASC

which has this result:

Comanda    | Total   | InStoc | Picked
-----------|------------------|--------
 AAY280838 |   NULL  | NULL   |   55
 AAY280837 |   NULL  | NULL   |   39
 AAY280836 |   NULL  | NULL   |   76

Could you please help me with the query so I can get this result?

Comanda    | Total | InStoc | Picked
-----------|----------------|--------
 AAY280838 |   64  | 6      |   55
 AAY280837 |   50  | 8      |   39
 AAY280836 |   89  | 8      |   76

Thanks in advance.

SELECT t1.Comanda,t1.Total,t1.InStoc,t2.Picked
(
SELECT
orders_list.nr_comanda_orders AS Comanda,
SUM(orders_list.cantitate) AS Total, 
SUM(orders_list.qty_to_add) AS InStoc,

FROM orders_list 
LEFT JOIN entries_list ON (entries_list.file_id = orders_list.id AND entries_list.orders_list_id = orders_list.nr_comanda_orders)
GROUP BY orders_list.nr_comanda_orders
) t1

LEFT JOIN

(

SELECT
entries_list.orders_list_id AS Comanda,
COUNT(entries_list.id) AS Picked
FROM  entries_list 
LEFT JOIN orders_list ON (entries_list.file_id = orders_list.id AND entries_list.orders_list_id = orders_list.nr_comanda_orders) 
LEFT JOIN orders_uploaded ON orders_list.file_id = orders_uploaded.id
GROUP BY 
entries_list.orders_list_id  

) t2
ON t1.Comanda = t2.Comanda

Lazy, slow performing solution:

SELECT 
  first.Comanda AS Comanda,
  first.Total AS Total,
  first.InStoc AS InStoc
  second.Picked AS Picked
FROM (
    -- your first query here
  ) AS first
  LEFT JOIN (
    -- your second query here
  ) AS second
  ON first.Comanda=second.Comanda
ORDER BY
  -- whatever

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM