多次加入codeigniter不要误解

时间:2018-08-25 15:57:28

标签: php mysql sql codeigniter join

您能帮我解决我的错误吗?

我试图加入三个表:Users,Crush和Discrush。

但是此代码未找到结果。

数据库:

  • 用户:id和内容
  • 粉碎:id,user_id,user_id_to
  • Discrush:id,user_id,user_id_to

查询:

SELECT
  *
FROM
  `users`
  RIGHT JOIN `discrush` ON `users`.`id` = `discrush`.`user_id_to`
  RIGHT JOIN `crush` ON `users`.`id` = `discrush`.`user_id_to`
WHERE
  `users`.`sexe` = 'female'
  AND `users`.`valid_admin` = '1'
  AND `users`.`id` NOT IN('81')
  AND `crush`.`user_id_to` is NULL
  OR `users`.`sexe` = 'female'
  AND `users`.`valid_admin` = '1'
  AND `users`.`id` NOT IN('81')
  AND `discrush`.`user_id_to` is NULL
ORDER BY
  RAND()

型号:

function GetUserForCrushRoom($my_user_id, $sexe)
{
    $this->db->select('*');
    $this->db->from('users');
    $this->db->join('discrush', 'users.id = discrush.user_id_to', 'right');
    $this->db->join('crush', 'users.id = discrush.user_id_to', 'right');

    $this->db->where('users.sexe =', $sexe);
    $this->db->where('users.valid_admin =', '1');
    $this->db->where_not_in('users.id', $my_user_id);
    $this->db->where('crush.user_id_to is NULL');

    $this->db->or_where('users.sexe =', $sexe);
    $this->db->where('users.valid_admin =', '1');
    $this->db->where_not_in('users.id', $my_user_id);
    $this->db->where('discrush.user_id_to is NULL');
    $this->db->order_by('users.id', 'RANDOM');
    $query = $this->db->get();
    return $query->row();

}

控制器:

public function crush_room()
{
    $my_user_id = $this->session->userdata('user_id');
    if($my_user_id != '')
    {
        $data['my_user_id'] = $my_user_id;
        $my_user = $this->home->get_user($my_user_id);

            $sexe = $my_user->sexe;

            if($my_user->sexe == 'male')
            {
                $sexe = 'female';
            }else{
                $sexe = 'male';
            }

            $data['user'] = $this->home->GetUserForCrushRoom($my_user_id, $sexe);

            if($data['user']){
                $data['user_crush'] = $this->home->CheckCrush($my_user_id, $data['user']->id);
                $data['user_block'] = $this->home->CheckBlockUser($my_user_id, $data['user']->id);
            }

            $data['users'] = $this->home->get_all_user_crush($my_user_id);

            $this->load->view('crush_room', $data);
    }
    else{
        redirect('login');
    }
}

2 个答案:

答案 0 :(得分:1)

我搞定了!

以此替换查询...

$sql = "SELECT * FROM users U WHERE U.sexe = ? AND U.valid_admin = '1' AND U.id NOT IN (SELECT user_id_to FROM discrush UNION SELECT user_id_to FROM crush)";

    $query = $this->db->query($sql, array($sexe));

        return $query->row();

答案 1 :(得分:0)

尝试与用户一起加入迷恋。

class Dentist < Person extend Organizable include organizable(DentistClinic) end 上正确加入crushusers = idcrush