I'm creating a mysql statement that uses a left join but need to exclude a duplicate field from the second table. Is there an easy way to do this?
My query looks like this:
SELECT * FROM profiles LEFT JOIN login_users ON profiles.user_id=login_users.user_id WHERE profiles .user_id={$user_id}
Both the profiles and login_users table have a 'gender' field but I only want the 'gender' field from the profile table.
You might just want to define what you actually need, instead of using a *
. THat's kinda good practice and all.
But if you REALLY need that *
, you can just do something like this:
SELECT *,profiles.gender as TheRealGenderINeed
FROM profiles
LEFT JOIN login_users ON profiles .user_id=login_users.user_id
WHERE profiles .user_id={$user_id}
Now you've got an extra genderfield available as TheRealGenderINeed
. If you don't want extra fields you should really just specify what fields you want from each table. That way you'll only get one gender if you only specify one ;)