Mysql 行间比较
如何将表中的所有行值(总和)与第一行(即第一个日期)进行比较 例如:Mysql 行间比较,mysql,sql,Mysql,Sql,如何将表中的所有行值(总和)与第一行(即第一个日期)进行比较 例如: ID Date Sum 1 01-01-2020 60 2 01-02-2020 70 3 01-05-2020 80 4 01-06-2020 25 我想要所有的ID,其中的总和大于60(第一个日期) 我试图将第一个日期设置为min(日期),但无法比较日期内的总和 结果应该是: ID Date Sum 2 01-02-2020 70 3 01-05-2020
ID Date Sum
1 01-01-2020 60
2 01-02-2020 70
3 01-05-2020 80
4 01-06-2020 25
我想要所有的ID,其中的总和大于60(第一个日期)
我试图将第一个日期设置为min(日期),但无法比较日期内的总和
结果应该是:
ID Date Sum
2 01-02-2020 70
3 01-05-2020 80
在MySQL 8+中,您可以使用
第一个\u值()
:
然后,您可以将其合并到子查询中,以获取等于或超过该值的行:
select t.*
from (select t.*, first_value(sum) over (order by date) as first_sum
from t
) t
where sum > first_sum;
您也可以通过交叉连接来执行此操作:
select t.*
from t cross join
(select t.*
from t
order by date asc
limit 1
) t1
where t.sum > t1.sum;
或者对where
子句中的子查询执行相同的操作:
select t.*
from t
where t.sum > (select t2.sum
from t t2
order by t2.date
limit 1
);
你是说两张桌子吗??
select t.*
from t
where t.sum > (select t2.sum
from t t2
order by t2.date
limit 1
);
Select * from mytable where sum > (select sum from mytable order by date limit 1).