MySQL根据给定的参数显示最早的副本

时间:2012-04-02 15:02:06

标签: bash mysql sql

我需要声明的是:

  1. 从名称中输出最早日期的重复数据。
  2. 例如:

    如果mich.edu的日期为4/2/2012,mich.edu的日期为4/1/2012,我希望该语句仅输出mich.edu 4/1/2012

    以下是我的发言:

    echo "select name, reported_at from nodes where reported_at < curdate() or reported_at is null;" | mysql dashboard
    

    当前输出:

    name            reported_at
    ngsghsdfg.edu   2012-03-23 03:40:04
    wasdfas.edu 2012-03-05 17:42:03
    cnadfafg.uiowa.edu  NULL
    qwerqwer.edu    2012-03-19 17:03:03
    qwerqwre.edu    2012-03-30 20:04:02
    qewrqwre.uiowa.edu  2012-03-24 16:10:02
    qwerqewr.edu    2012-03-23 22:12:05
    qwrewqreq.uiowa.edu NULL
    qwerwqer.edu    2012-04-01 04:18:05
    adfgnafg.edu    2012-03-27 18:21:01
    adsfasf.edu         NULL
    

2 个答案:

答案 0 :(得分:2)

听起来你需要min() aggregate function

echo "select name, min(reported_at) from nodes where reported_at < curdate() or reported_at is null group by name;" | 
mysql dashboard

答案 1 :(得分:0)

select name, reported_at from nodes where reported_at < curdate() or reported_at is null order by reported_at desc group by name ;

也可能适合你,因为order by比较日期,而不是字符串(min还没有)