'SQL,how to select attributes from different tables?
i want so select user_name,vehicle_name and maintain_cost from 3 different tables where user_id,vehicle_id and maintain_cost_id is equal to three,so which query i used,i try this
(select user_name,vehicle_name,maintain_cost
from user,vehicle,maintain_cost
where user_id='3')
but do not know how to put vehicle_id='3' and maintain_cost_id='3'
Solution 1:[1]
Use a left join, an example can be found here: http://www.w3schools.com/sql/sql_join_left.asp
Solution 2:[2]
You can do something like this, just adapt it with the real ids:
SELECT user_name,vehicle_name,maintain_cost
FROM USER u
LEFT JOIN vehicle v ON v.user_id=u.user_id
LEFT JOIN maintain_cost m ON m.vehicle_id=v.vehicle_id
WHERE u.user_id=3 AND v.vehicle_id=3 AND maintain_cost_id=3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DrRoach |
| Solution 2 | aimstone |
