选择具有不同参数的最新记录行

时间:2013-11-27 12:25:18

标签: mysql select group-by distinct union

我想选择一个非重复记录列表,这些记录会激活某个用户(user_touser_from)。我想检索其他用户以及该记录中的最新内容。该列表在所选的其他用户中不得有重复。

例如,我有以下记录集

id   user_to   user_from   content     time
1      1          2          ABC    2013-11-05  
2      4          2          BBC    2013-11-06  
3      3          1          CBC    2013-11-07  
4      5          1          ABC    2013-11-08 
5      1          2          AAC    2013-11-09  
6      5          1          ABB    2013-11-10 
7      3          4          CBC    2013-11-11  
8      1          2          ACC    2013-11-12 

在这种情况下,如果要提供的参数为1,我想选择记录3,6,8,其他未选中,因为它们是重复的和较旧的,或者它们不涉及1。 我调查了this post并尝试过这样的事情:

 SELECT u, content, date FROM(
       (SELECT 
                user_from AS u, 
                MAX(time) AS date, 
                content
            FROM t1 
                WHERE user_to = :user 
            ) 
                UNION
            (SELECT 
                user_to AS u, 
                MAX(time) AS date, 
                content
            FROM t1  
                WHERE user_from = :user 
            ) 
        ) t2
WHERE date IN (SELECT MAX(date) FROM t2 GROUP BY u)

但不,不能完成它。

知道如何编写查询吗?谢谢!

3 个答案:

答案 0 :(得分:0)

您的查询应该是:

 select m.* from 
 message m,
 ( select user_to,
          user_from, 
          max(dtime) mxdate 
     from message 
    where user_from = 1 or user_to = 1
    group by user_to, user_from) m2
 where m.dtime = m2.mxdate
   and (m.user_from = 1 or m.user_to = 1)

在小提琴中查看:http://sqlfiddle.com/#!2/13d4e/4

正如你在评论中提出的那样:好的。但由于我只想要其他用户的user_id,有没有办法在user_from = 1时选择user_to而在user_to = 1时只选择user_from?

select if(m.user_to=1,m.user_from,m.user_to) as user, 
       m.content, 
       m.dtime
  from 
    message m,
    ( select user_to,
             user_from, 
             max(dtime) mxdate 
        from message 
       where user_from = 1 or user_to = 1
       group by user_to, user_from) m2
 where m.dtime = m2.mxdate
   and (m.user_from = 1 or m.user_to = 1)

在此处查看:http://sqlfiddle.com/#!2/13d4e/5

答案 1 :(得分:0)

如果过滤为“1”,请将其添加到@Jorge Campos

的查询中
where user_from = 1 OR user_to=1

答案 2 :(得分:0)

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id   INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,user_to   INT NOT NULL
,user_from INT NOT NULL  
,content   CHAR(3) NOT NULL  
,dt DATE NOT NULL
);

INSERT INTO my_table VALUES
(1,1,2,'ABC','2013-11-05'),
(2,4,2,'BBC','2013-11-06'),
(3,3,1,'CBC','2013-11-07'),
(4,5,1,'ABC','2013-11-08'),
(5,1,2,'AAC','2013-11-09'),
(6,5,1,'ABB','2013-11-10'),
(7,3,4,'CBC','2013-11-11'),
(8,1,2,'ACC','2013-11-12'); 

SELECT x.* 
  FROM my_table x 
  JOIN 
     ( SELECT LEAST(user_to,user_from) l
            , GREATEST(user_to,user_from) g
            , MAX(dt) max_dt FROM my_table 
        GROUP 
           BY LEAST(user_to,user_from)
            , GREATEST(user_to,user_from)
     ) y 
    ON y.l = LEAST(x.user_to,x.user_from) 
   AND y.g = GREATEST(x.user_to,x.user_from) 
   AND y.max_dt = x.dt 
 WHERE 1 IN (x.user_to,x.user_from);
+----+---------+-----------+---------+------------+
| id | user_to | user_from | content | dt         |
+----+---------+-----------+---------+------------+
|  3 |       3 |         1 | CBC     | 2013-11-07 |
|  6 |       5 |         1 | ABB     | 2013-11-10 |
|  8 |       1 |         2 | ACC     | 2013-11-12 |
+----+---------+-----------+---------+------------+