【发布时间】:2012-11-17 19:06:31
【问题描述】:
我有 3 张桌子,
user - contains user_id | username | fullname | email etcc
user_profile - contains entry_id |user_id| profile_image_id| user_location etcc
user_profile_images - contains image_id| user_id | file_path | file_thumb | date etcc
当一个用户注册时,所有的细节都进入到用户表中,user_id 也被添加到 user_profile 表中,其余的设置默认为 NULL,直到用户添加一些配置文件信息。
我在 model_users 中有一个查询来从这三个表中获取所有用户的数据,它是这样的;
$this->db->where('users.user_id', $user_id)->select('*')->from('users');
$this->db->join('user_profile', 'user_profile.user_id = users.user_id', 'left');
$this->db->join('user_profile_images','user_profile_images.image_id = user_profile.profile_image_id','left');
只有当 user_profile 中的 profile_image_id 字段不为空时,即用户上传了图片时,它才能正常工作。在该字段为空的情况下,即使返回所有其他数据,也不返回用户user_id。
我知道为什么会这样,因为我的联接查询需要字段 profile_image_id 但目前我的解决方法是将 user_profile 中的 profile_image_id 设置为 1(默认),这是默认图像,并且 image_id 为 1 和 user_id 0 因为它是一般的默认图像。但我仍然无法克服这样一个事实,即我需要更新该查询以使其不再是黑客攻击。 你们有什么想法吗?
【问题讨论】:
-
尝试将 user_profile_images 加入更改为“正确”加入?
-
感谢回复,还是不行:(
-
最好的办法是关闭这个问题,将查询转换为原始 MySQL 查询,然后在 SO 上发布该查询。这里有很多 mySQL 专家 - 但我怀疑他们会以当前格式知道这个问题的答案。
-
只是不要使用 left 并将这个 user_profile_images.image_id = user_profile.profile_image_id 改为这个 user_profile.profile_image_id = user_profile_images.image_id 然后检查你是否也设置了索引
-
@Ispuk 为什么左连接会成为问题?实际上,左连接通常会获得最快的连接语法性能。
标签: mysql codeigniter activerecord codeigniter-2