我有三张桌子
用户:
第二张表
朋友
和最后一个
CI_Sessions
我想要Codeignier查询
select * from users where userid=$userid
select * from friends where myid=$userid or friend_id=$userid
select * from ci_sessions where user_id=$userid and user_data!=''
返回结果()
可以在一个声明中得到所有三个吗?
我正在尝试这样
function check_friends($username) {
$this->db->where('username',$username);
$q1 = $this->db->get('users');
$myid=$q1->row('userid');
if($username !='' ){
$this->db->select('*');
$this->db->from('users');// I use aliasing make joins easier
$this->db->where('userid',$myid);
$this->db->join('myfriends AS friends_tpl', 'friends_tpl.myid = '.$myid.' or friends_tpl.fid='.$myid.'', 'FULL');
$this->db->join('ci_sessions AS B', 'B.id_unite = A.userid', 'INNER');
$this->db->where('B.user_data !=','');
$this->db->where('B.id_unite !=','0');
$query = $this->db->get();
return $query->result();
}else{
}
}
更新
我尝试这个查询
$query = $this->db->query("select * from users as u inner join myfriends as f on f.fid = '".$this->session->userdata('userid')."' or f.myid= '".$this->session->userdata('userid')."' inner join ci_sessions as c on c.id_unite = u.userid where u.username != '$username' GROUP BY u.userid ");
但是有问题,这个查询不是我想要的正确结果,
这是朋友表值
现在如果三个用户登录,查询将显示用户54是用户57的朋友我不知道w7y
答案 0 :(得分:0)
这样做
$query = $this->db->query( 'SELECT * -----
UNION ALL
SELECT * -----
UNION ALL
SELECT *---' );
这将给出所有结果......