codeigniter中的多个连接不起作用

amrnrhlw  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(663)

你能帮我修理我的窃听器吗?
我试着加入三个表:users、crush和discrush。
但此代码未找到任何结果。
数据库:
用户:id等等
crush:id,user\u id,user\u id到
discrush:id,user\u id,user\u id到
查询:

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');
    }
}
5uzkadbs

5uzkadbs1#

我搞定了!
将查询替换为。。。

$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();
2wnc66cl

2wnc66cl2#

尝试加入crush与用户。
右连接 crushusers . id = crush . user_id_to

相关问题