sql剔除某一时间以后某一个条件的数据

数据库字段,


```八月份表

name   sex   age   address      time
张三    男    19     快手     1627747200000
李四    女    18     抖音     1627920000000
王五    男    18     微视     1628179200000
赵六    男    18     腾讯     1628956800000
田七    男    18    爱奇艺    1630252800000

```九月份表

name   sex   age   address      time
张三    男    19     快手     1631116800000
李四    女    18     抖音     1630512000000
王五    男    18     微视     1630425600000
赵六    男    18     腾讯     1631462400000
田七    男    18    爱奇艺    1631635200000

九月份快手、抖音都失效

两张表用一个sql,查询九月以后的数据时过滤快手和抖音失效的数据,但是查询八月的时候还是可以查到快手和抖音的数据

请问这个sql该怎么写呢?谢谢

用一个sql,那区分不同表的依据是什么?外部来的条件吗?

这是Oracle下的答案
答案如下:自己改一下日期即可,需要同时将三个日期都改成相同的才行,由于你使用的是13位数字时间戳,所以我先将其转换为了date类型,这样方便计算。8月的时候查询全部,9月的时候查询9月+8月数据同时过滤了8月失效的抖音和快手
如果是其他数据库就麻烦你自己改一下了。with是Oracle特有写法相当于视图,你可以改成内联视图,这样其他数据库也支持。
核心思路如下:当前日期和数据库中的数据一致时则显示全部,同时如果当前日期和数据日期不一致时则踢出过期数据,trunc的作用就是抽取日期中的月份,将不管当前日期是几号都转换为当月的第一天。


with ta as(
               
select '张三' name ,  '男' sex  ,  19 age  ,  '快手'  address ,   1627747200000 time from dual
union all
select '李四' name ,  '女'sex   , 18  age  , '抖音' address   , 1627920000000 time from dual
union all
select '王五' name ,  '男'  sex ,  18  age ,  '微视' address  ,  1628179200000 time from dual
union all
select '赵六' name ,  '男' sex  , 18  age  , '腾讯'  address  , 1628956800000 time from dual
union all
select '田七' name ,  '男' sex  , 18  age , '爱奇艺' address  , 1630252800000time from dual
)
,tb as(
               
select '张三' name ,  '男' sex  ,  19 age  ,  '快手'  address ,   1631116800000 time from dual
union all
select '李四' name ,  '女'sex   , 18  age  , '抖音' address   , 1630512000000 time from dual
union all
select '王五' name ,  '男'  sex ,  18  age ,  '微视' address  ,  1630425600000 time from dual
union all
select '赵六' name ,  '男' sex  , 18  age  , '腾讯'  address  , 1631462400000 time from dual
union all
select '田七' name ,  '男' sex  , 18  age , '爱奇艺' address  , 1631635200000 from dual
)
,t8 as(
select ta.*, to_date('1970/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss')+ta.time/(1000*60*60*24) time1 from ta 
),t9 as(
select tb.*, to_date('1970/01/01 08:00:00', 'yyyy/mm/dd hh24:mi:ss')+tb.time/(1000*60*60*24) time1 from tb  
)
select * from t8 where 
(trunc(time1,'mm') != trunc(TO_DATE('2021-8-10','YYYY-MM-DD'),'mm')  AND T8.address NOT IN('快手','抖音'))
or (trunc(time1,'mm') = trunc(TO_DATE('2021-8-10','YYYY-MM-DD'),'mm'))
union all
select * from t9 where trunc(time1,'mm') = trunc(TO_DATE('2021-8-10','YYYY-MM-DD'),'mm')
8月结果如下:
1    张三    男    19    快手    1627747200000    2021/8/1
2    李四    女    18    抖音    1627920000000    2021/8/3
3    王五    男    18    微视    1628179200000    2021/8/6
4    赵六    男    18    腾讯    1628956800000    2021/8/15
5    田七    男    18    爱奇艺    1630252800000    2021/8/30

9月结果如下:
1    王五    男    18    微视    1628179200000    2021/8/6
2    赵六    男    18    腾讯    1628956800000    2021/8/15
3    田七    男    18    爱奇艺    1630252800000    2021/8/30
4    张三    男    19    快手    1631116800000    2021/9/9
5    李四    女    18    抖音    1630512000000    2021/9/2
6    王五    男    18    微视    1630425600000    2021/9/1
7    赵六    男    18    腾讯    1631462400000    2021/9/13
8    田七    男    18    爱奇艺    1631635200000    2021/9/15