I have a situation where I have three tables; dealership, car and warehouse. Dealership is related to car and car is related to warehouse. How can a mysql query be constructed to determine the sum of the number of vehicles in the warehouses that were not sent to a dealer?
Here are the data tables:
Dealer
Id | name |
_____|____________|
D1 | Dealer_1 |
D2 | Dealer_2 |
D3 | Dealer_3 |
D4 | Dealer_4 |
Car
Id | name | Dealer_id |
_____|___________|___________|
C1 | auto_1| | D4 |
C2 | auto_2 | D4 |
C3 | auto_3 | null |
C4 | auto_4 | D1 |
C5 | auto_5 | null |
C6 | auto_6 | D3 |
Warehouse
Id | name | Car_id | vehical_count |
_____|___________|___________|_______________|
W1 | storage_1 | C2 | 22 |
W2 | storage_2 | C3 | 43 |
W3 | storage_3 | C5 | 18 |
W4 | storage_4 | C6 | 15 |
W5 | storage_5 | C1 | 8 |
W6 | storage_6 | null | 2 |
W7 | storage_7 | C3 | 37 |
W8 | storage_8 | null | 10 |
Thanks!
This should give you warehouse cars not in the car table or in the car table with no dealer assigned.
SELECT
SUM(w.vehical_count)
FROM
Warehouse w
LEFT OUTER JOIN Car c ON c.Id=w.Car_id
WHERE
c.Dealer_id IS NULL
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.