I have two tables, a vehicle
table with columns:
id
stock
year
make
Task: Print 3 columns table:
Expected output (just an example):
+----------------------+----------------+--------------+
| title | comments_count | images_count |
+----------------------+----------------+--------------+
| BMW X6 | 35 | 9 |
| Audi A6 | 3 | 5 |
| Volkswagen Passat B6 | 78 | 6 |
| Volkswagen Passat B5 | 129 | 4 |
+----------------------+----------------+--------------+
Solution:
SELECT
vehicles.title,
(SELECT COUNT(*) FROM comments WHERE vehicles.id = comments.vehicle_id) AS comments_count,
(SELECT COUNT(*) FROM images WHERE vehicles.id = images.vehicle_id) AS images_count
FROM vehicles