首页  >  问答  >  正文

mysql - 求一条SQL语句

---------------------------
| from | to | text | time |
---------------------------
| seg | me | ye! | 4 |
---------------------------
| seg | me | ye! | 3 |
---------------------------
| seg | yu | ye! | 2 |
---------------------------
| seg | yu | ye! | 1 |
---------------------------

大概是这个样子的一个表,我想知道能不能用一条语句提取出from=seg下所有不同的to字段值的time最新的一条数据,如果不能一句话的话写成几句也成0_0,比如上面应该获取到

---------------------------
| from | to | text | time |
---------------------------
| seg | me | ye! | 4 |
---------------------------
| seg | yu | ye! | 2 |
---------------------------

这两条数据。

怪我咯怪我咯2764 天前297

全部回复(2)我来回复

  • 怪我咯

    怪我咯2017-04-17 11:11:08

    使用group by 汇总即可

    select t.* from t , (
          select c_from,c_to,max(c_time ) as c_time
          from t
          where 
          c_from = 'seg'
          group by c_from,c_to ) t1
    where 
    t.c_from = t1.c_from 
    and t.c_to = t1.c_to
    and t.c_time = t1.c_time

    回复
    0
  • PHP中文网

    PHP中文网2017-04-17 11:11:08

    SELECT   a.*
      FROM   user_post a, (  SELECT   content, MAX (post_date) post_date
                               FROM   user_post
                              WHERE   user_name = 'withrock'
                           GROUP BY   content) b
     WHERE   a.content = b.content AND a.post_date = b.post_date

    这张表的结构和你的不太一样,不过描述的问题是一样的.

    回复
    0
  • 取消回复