我有下面提到的数据库:
ID TFD1 Date CCA
D-1 123 25-01-2017 AA
D-2 123 24-01-2017 BA
D-3 123 27-01-2017 BB
D-4 456 15-01-2017 AA
D-5 456 13-01-2017 BS
D-6 789 11-01-2017 AA
我想获取至少有一个值的 ID
、TFD1
、Date
和 CCA
对应于TFD1
的是AA
,并且对应于TFD1
的所有其他旧值不是AA
。
我想创建按最旧到最新排序的输出。
所需输出:
ID TFD1 Date CCA
D-6 789 11-01-2017 AA
D-1 123 24-01-2017 BA
D-2 123 25-01-2017 AA
D-4 456 13-01-2017 BS
D-5 456 15-01-2017 AA
最佳答案
要获得重要的“A”,您可以使用:
select t.*
from t
where (t.cca = 'A' and
not exists (select 1 from t t2 where t2.tfd1 = t.tfd1 and t.cca = 'A')
) ;
从此您可以获得每个 tfd1
在此日期之前的所有内容:
select t.*
from t join
(select t.*
from t
where (t.cca = 'A' and
not exists (select 1
from t t2
where t2.tfd1 = t.tfd1 and t.cca = 'A' and t2.date < t1.date
)
)
) tt
on t.tfd1 = tt.tfd1 and t.date <= tt.date;
关于mysql - 从Sql中获取对比数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49322067/