Welcome Guest, Not a member yet? Register   Sign In
select all users SQL [RESOLVED]
#1

[eluser]kolxoznik1[/eluser]
I am trying to get all users which I follow with their username and images

I have three tables : users / images / user follow

In "user_follow" table I keep users id which one follow

here is my query:
Code:
$this->db->select($this->users_table.'.username');

$this->db->select('images.image');
        
$this->db->from($this->table);
        
$this->db->join($this->users_table, $this->table.'.follow_id = '.$this->users_table.'.id');
        
$this->db->join('images', $this->table.'.follow_id = '.$this->image_table.'.user_id');
        
$this->db->where('user_follow.user_id',$user_id);
        
$query = $this->db->get();
        
if ($query->num_rows() > 0)
{
return $query->result_array();
}
return FALSE;

I get all users_id which I follow from "user follow" and after try to get their "username" and "image" from their id.

Problem is that query show user only if he have image if no then query do not show line at all (not username)

I am trying to do that show all users which I follow and their images (if user have it) if no then do not show?

is it possible to do?

I found some good sql queries

Code:
select * from users u
  left outer join images i on u.id = i.user_id
  left join user_follow f on f.follow_id = u.id
where  f.userid = 3

Code:
SELECT
    u.username
  , i.image
FROM
  users u
  -- normal join of users and user_follow
  INNER JOIN user_follow f on (u.id = f.follow_id)
  -- include images if they exist, null otherwise
  LEFT OUTER JOIN images i on (f.follow_id = i.user_id)
WHERE
  f.user_id = 3
;





Theme © iAndrew 2016 - Forum software by © MyBB