I have written this code to get some info from my database.
"SELECT
c.from AS user_id,
c.time AS time,
u.user_firstname AS user_firstname,
u.user_lastname AS user_lastname,
u.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u ON u.user_id = c.from WHERE c.to = :id1
UNION SELECT
c.to AS user_id,
c.time AS time,
u2.user_firstname AS user_firstname,
u2.user_lastname AS user_lastname,
u2.user_profile_picture AS user_profile_picture
FROM chat c INNER JOIN users u2 ON u2.user_id = c.to WHERE c.from= :id2
ORDER BY time DESC"
it's work great except one thing. Since this is a inbox script there are many message from same user. but i only need to check if there is a message from this user or not.
array (size=28)
0 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 19:53:58' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg' (length=19)
1 =>
array (size=5)
'user_id' => int 2
'time' => string '2014-05-13 16:59:50' (length=19)
'user_firstname' => string 'james' (length=5)
'user_lastname' => string 'bond' (length=4)
'user_profile_picture' => string '2_user_profile.jpg' (length=19)
2 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 02:15:44' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg' (length=19)
3 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 02:13:21' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg'(length=19)
4 =>
array (size=5)
'user_id' => int 2
'time' => string '2014-05-13 01:58:59' (length=19)
'user_firstname' => string 'james' (length=5)
'user_lastname' => string 'bond' (length=4)
'user_profile_picture' => string '2_user_profile.jpg'(length=19)
as you can see in the var_dump there are 3 john doe and 2 james bond. but I need only the last ones according to the time. so in this case john doe from 19:53:58, and james bond from 16:59:50. Like this:
array (size=2)
0 =>
array (size=5)
'user_id' => int 6
'time' => string '2014-05-13 19:53:58' (length=19)
'user_firstname' => string 'john' (length=4)
'user_lastname' => string 'doe' (length=3)
'user_profile_picture' => string '6_user_profile.jpg' (length=19)
1 =>
array (size=5)
'user_id' => int 2
'time' => string '2014-05-13 16:59:50' (length=19)
'user_firstname' => string 'james' (length=5)
'user_lastname' => string 'bond' (length=4)
'user_profile_picture' => string '2_user_profile.jpg' (length=19)
and if there are some other users i want to get their last records too. how can i do this? is this possible with only one query?