sql query to get two differnt values of same columnsAsk Question

问题:

I am making an activity log for my website and to show the logs that user 1 followed user 2, I have their ids in my activity_log table. Now I want to retrieve their names from user_login table.

For example user_id=3;friend_id=6;. I want their username so that in activity log I can print "$user followed $friend". So I want to know about the query so that I can retrieve usernames using two variables, suppose variables are $finalres1=3;$finalres2=6; How do I fetch their usernames from table user_login with a single query

foreach($results as $res=>$finalres){
    $finalres1=$finalres['user_id'];
    $finalres2=$finalres['friend_id'];

    $this->db->select('username');
    $this->db->from('user_login');
    $this->db->where('id',$finalres1);

    $query=$this->db->get();
    $result3=$query->result_array();

this query fetches me the username of user 1 but I need username of both the user and friend in single array.so in the view I can write them easily.

回答1:


add that

$this->db->or_where('id',$finalres2)

回答2:


add that $this->db->select('username as username1'); $this->db->from('user_login'); $this->db->where('id',$finalres1);

标签: mysql codeigniter rdbms codeigniter-3
© 2014 TuiCode, Inc.