php - how to get two rows from a table using join -
this code
$query = $this->db ->select('*') ->join('users receiver', 'receiver.user_id = messages.user1') ->join('users sender', 'sender.user_id = messages.user2') ->get('messages')->result_array(); this database
users table user_id |username | datetime 1 | abc1 | 000000000 2 | abc2 | 000000000 messages table msg_id | user1 | user2 | msg | timestamp 1 | 1 | 2 | hello | 000000000 i want records of 2 users sender , receiver. join give me record of last join. in case m getting record of sender. want output this
[0] => array ( [user1] => stdclass object ( [id] => 1 [username] => abc1 [datetime] => 2016-11-07 03:00:00 ) [user2] => stdclass object ( [id] => 2 [username] => abc2 [datetime] => 2016-11-07 00:00:00 ) [message] => stdclass object ( [id] => 1 [message] => hdf [timestamp] => 2016-11-06 08:43:26 ) ) thanks in advance
here sql create union query give 2 rows
select * messages m join users u on u.user_id = m.user1 union select * messages m join users u on u.user_id = m.user2; or join statement 2 users in 1 row
select * messages m join users u1 on u1.user_id = m.user1 join users u2 on u2.user_id = m.user2;
Comments
Post a Comment