从结果集中选择最新记录(mysql,php)

时间:2014-05-14 03:35:49

标签: php mysql sql

我已编写此代码以从我的数据库中获取一些信息。

       "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"
除了一件事,它的工作很棒。由于这是一个收件箱脚本,因此来自同一用户的消息很多。但我只需要检查是否有来自该用户的消息。

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)

正如你在var_dump中看到的那样,有3个john doe和2个james bond。但我根据时间只需要最后一个。 所以在这种情况下,john doe从19:53:58开始,james bond从16:59:50开始。像这样:

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)

如果还有其他一些用户我也希望得到他们的最后一条记录。 我怎样才能做到这一点?只有一个查询可以实现吗?

3 个答案:

答案 0 :(得分:1)

将联合放在子查询中,在主查询中对其进行排序,并将其限制在最近的行中。

SELECT *
FROM (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
LIMIT 1

答案 1 :(得分:1)

尝试我测试的那个。

 "SELECT *
    FROM (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 
) AS bynames
  GROUP BY user_id ORDER BY time ASC"

答案 2 :(得分:0)

未测试

SELECT c.user_id , MAX(c.time) , u.user_firstname , u.user_lastname , u.user_profile_picture FROM ( SELECT from AS user_id , time FROM chat WHERE to = :id1 UNION SELECT to AS user_id , time FROM chat WHERE from = :id2 ) as c JOIN users u ON u.user_id = c.user_id GROUP BY c.user_id , u.user_firstname , u.user_lastname , u.user_profile_picture