oxido
oxido

Reputation: 1

How to get SUM in Mysql from another table based on first table

---------------colete--------------------
| id | id_comanda  | status | id_lista |
----------------------------------------
|    | 21775       | 0      | 3820     |
----------------------------------------
|    | 21776       | 0      | 3820     |
----------------------------------------
|    | 21777       | 0      | 3820     |
----------------------------------------


-----comenzi--------
| id  | taxComanda |
--------------------
|21775| 16.00      |
--------------------
|21776| 00.00      |
--------------------
|21777| 16.00      |
--------------------

I want to get SUM from column taxaComand from table comenzi by making the selection from column id_lista from table colete, so the SUM at the end to be 32.00

This is what i have, but is not good.:

SELECT a.id_comnda AS a_id_comanda, a.id_lista AS a_id_lista,
b.id AS b_id, b.taxaComanda AS b_taxaComanda
(SELECT sum(taxaComanda) FROM comenzi WHERE b.id = a.id_comanda AS totalTaxaComanda)
FROM colete a
INNER JOIN comenzi b ON b.id = a.id_comanda
WHERE a.id_lista = 3820
GROUP BY a.id_comanda

Upvotes: 0

Views: 71

Answers (2)

tyro
tyro

Reputation: 1428

Using subquery and grouping id_lista:

SELECT id_lista, SUM(taxComanda ) as result,
FROM(
     SELECT id_comanda  , status , id_lista, taxComanda 
     from colete
     left join comenzi  on colete.id = comenzi.id_comanda)as sq
group by sq.id_lista

Upvotes: 0

Thomas G
Thomas G

Reputation: 10206

Based on your comment I see no other answer than this :

SELECT SUM(b.taxComanda )
FROM colete a
INNER JOIN comenzi b ON b.id = a.id_comanda
WHERE a.id_lista = 3820

Upvotes: 1

Related Questions