I am working on expanding user profiles for my project, adding more info. I was just adding countries, when I hit a problem. This is my query for selecting all profile data needed:
SELECT c.*, d.username, d.email, e.country_name
FROM user_profiles c, users d, country e
WHERE c.user_id = ".$id." AND d.id = ".$id."
AND e.country_name = (SELECT country_name FROM country WHERE id = c.country_id)
c.* should select all columns from user_profiles.
The $id
is an id of user selecting wich profile should be returned. The problem is, keeping registration as simple as possible, I dont have a country selector there. So when this query comes in action, column country_id from user_profiles is empty, that means when I want to return the country_name, the query result is empty, so no profile data is returned.
I have tried to rewrite this with CASE
or LEFT JOIN
, but I think I am missing something. I want to find if country_id is not null, when it is not, select also country_name
with this country_id
. Any ideas?
Thank you kindly for responds.