我正在创建一个使用左连接但需要从第二个表中排除重复字段的mysql语句。有一个简单的方法吗?
我的查询如下:
SELECT * FROM profiles LEFT JOIN login_users ON profiles.user_id=login_users.user_id WHERE profiles .user_id={$user_id}
个人资料和login_users表都有一个“性别”字段,但我只想要个人资料表中的“性别”字段。
答案 0 :(得分:4)
您可能只想定义实际需要的内容,而不是使用*
。这是一种很好的做法和所有。
但如果你真的需要*
,你可以这样做:
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}
现在您有一个额外的性别字段TheRealGenderINeed
。如果你不想要额外的字段,你应该只是从每个表中指定你想要的字段。这样,如果你只指定一个性别,你将只获得一个性别;)