All entries
Posted: Tue Dec 14, 2010 6:29 am
Hy!
I have two tables (users, cars) that are connected through a foreign key user_id in cars (= id in users - users.id).
Some users have a car selected for them (cars.user_id = users.id), but some haven't got (cars.user_id = NULL).
I was wondering, how can I get all users (id, name, surname) (with car selected or not) with SELECT? It's a bit tricky, because I need also all the data (id, name) from cars table, also in this query...
I tried with: $query = "SELECT id, name, surname, id, name FROM users, cars";
$query = "SELECT id, name, surname, id, name FROM users, cars WHERE users.id = cars.user_id";
Thanks for Your help!
I have two tables (users, cars) that are connected through a foreign key user_id in cars (= id in users - users.id).
Some users have a car selected for them (cars.user_id = users.id), but some haven't got (cars.user_id = NULL).
I was wondering, how can I get all users (id, name, surname) (with car selected or not) with SELECT? It's a bit tricky, because I need also all the data (id, name) from cars table, also in this query...
I tried with: $query = "SELECT id, name, surname, id, name FROM users, cars";
$query = "SELECT id, name, surname, id, name FROM users, cars WHERE users.id = cars.user_id";
Thanks for Your help!