Dani Ace Aguila
Dani Ace Aguila

Reputation: 1

Use sum() using condition on another table

I have these two table.

 -----------------------------------------------
|order_id | cust_id|  order_detail|  order_price|
|101      | 203    | Canon-XL     | 4500.00     |
|102      | 201    | Canon-XL     | 4500.00     |
|103      | 201    | Battery-L    | 850.00      |
|104      | 207    | EPSONL 120   | 5100.00     |
|105      | 205    | EPSONL 120   | 5100.00     |
|106      | 203    | Battery-S    | 5100.00     |
|107      | 204    | HP DESK-230  | 520.00      |
|108      | 206    | TRIPOD-XL    | 1550.00     |
 -----------------------------------------------
 ------------------------------
|cust_id| salary_id|    salary  |
|201    | 101      |    3200.00 |
|202    | 102      |    4100.00 |
|203    | 103      |    2000.00 |
|204    | 104      |    5100.00 |
|205    | 105      |    5100.00 |
|206    | 106      |    2500.00 |
|207    | 107      |    2700.00 |
 ------------------------------

Now I want to sum the order_price on tb1 where salary on tb2 is less than 3000..

How can I do this?

 ------------
|total_order |
|------------|
|11150       |
 ------------

Upvotes: 0

Views: 33

Answers (1)

Mureinik
Mureinik

Reputation: 311978

You could join the two tables according to the cust_id:

SELECT SUM(order_price)
FROM   tb1
JOIN   tb2 ON tb1.cust_id = tb2.cust_id
WHERE  salary < 3000

Upvotes: 0

Related Questions