我有 2 张 table 想在当天加入。虽然日期格式相同,但我想将 t1.date_1
连接到 t2.date_2
,但其中 t2.date_2
是一个月前。请参阅下面的查询以了解上下文和我想要的输出:
t1
date_1 count_t1
2015-01-01 10
2015-02-01 20
2015-03-01 30
t2
date_2 count_t2
2014-12-01 40
2015-01-01 50
2015-02-01 60
2015-03-01 70
output i want:
date_1 percent_before
2015-01-01 0.25 <--10/40
2015-02-01 0.40 <--20/50
2015-03-01 0.5 <--30/60
my query:
select (t1.count_1/t2.count_2) as percent_before
from table1 t1 join table2 t2
on t1.date_1 = (t2.date_2 - 1 month);
最佳答案
您需要interval
关键字:
select t1.date_1, (t1.count_1 / t2.count_2) as percent_before
from table1 t1 join
table2 t2
on t1.date_1 = (t2.date_2 - interval 1 month);
关于mysql 连接日期,其中一个表的日期是一个月前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31390773/