SQL联接大于/小于
我有两张桌子 我 因此,我需要得到一个表,其中的行将被排除,每个id的日期都大于第二个表中的datestop 我尝试使用双重条件进行内部联接: id=id和DateSQL联接大于/小于,sql,date,join,left-join,inner-join,Sql,Date,Join,Left Join,Inner Join,我有两张桌子 我 因此,我需要得到一个表,其中的行将被排除,每个id的日期都大于第二个表中的datestop 我尝试使用双重条件进行内部联接: id=id和Date
select a.*
from tablea a
left join tableb b on b.id = a.id
where b.id is null or b.datestop > a.date
这句话的意思是:获取tablea中的所有记录,如果tableb中没有具有相同id的记录,或者其日期小于tableb中相应记录的datestop。这是一种所有记录都有帮助的情况:
select a.*
from a
where a.date < all (select b.datestop
from b
where b.id = a.id and b.status = 'Stopped'
);
我将使用非常有效的NOT EXISTS:
select t1.*
from table1 t1
where not exists (
select 1 from table2 t2
where t2.id = t1.id and t2.datestop <= t1.date
)
请在代码问题中给出一个-cut&paste&runnable代码;作为初始化代码的示例输入,所需和实际输出包括逐字错误消息;标签和版本;清晰的说明和解释。对于包含您能给出的代码最少的错误,即“显示为OK”的代码由“显示为not OK”的代码扩展。调试基础。对于包含版本和DDL的SQL,其中包括约束和索引以及表格初始化。请
where t2.id = t1.id and t2.datestop <= t1.date and t2.status = 'Stopped'