mysql,选择min表达式的源值而不是最小值

时间:2019-02-24 16:35:31

标签: mysql sql

我有一个查询说

String name;
String city;

public Person(String name, String city) {
    this.name = name;
    this.city = city;
}

@Override
public String toString() {
    return "Name : " + this.name + ", City : " + this.city;
}

@Override
public boolean equals(Object o) {

    Person person = (Person) o;
    if(person.name.equals(this.name)){
        return true;
    }

    return false;
}

但是我不希望最小值,我希望值d是产生最小值的输入。

这是在mysql 5.7中

2 个答案:

答案 0 :(得分:1)

最简单的方法可能是example.scss

row_number()

答案 1 :(得分:0)

Group by u获取所有最小值并加入表格:

select t.u, t.d
from tablename t inner join (
  select u, min(abs(TIMESTAMPDIFF(SECOND, d, '2018-12-07 23:00:00'))) minimumd
  from tablename 
  group by u
) g 
on g.u = t.u and g.minimumd = abs(TIMESTAMPDIFF(SECOND, t.d, '2018-12-07 23:00:00'))