通过将字段与单个其他记录进行比较来选择一个表中的记录

时间:2019-03-21 10:27:42

标签: select sqlite

我有两个SQLite表t1t2,它们具有相同的字段:namevalue1value2value3

至关重要的是,(a)表t1仅包含单个记录John|20|19|4,而(b)该记录可能会更改。

我想从T2中选择所有那些记录,其中t2.value1 <= t1.value1(即,唯一记录中的单个t1.value1)和t2.value2 <= t1.value2和t3.value3。这可能吗?

2 个答案:

答案 0 :(得分:1)

这应该做到:

select *
from T2
where exists 
    ( select * 
      from T1 
      where T2.Value1 <= T1.Value1 and 
            T2.Value2 <= T1.Value2 and 
            T2.Value3 <= T1.Value3
     )

答案 1 :(得分:0)

是的,有可能。您可以尝试以下查询

WITH (SELECT TOP(1) FROM table t1 AS record),
SELECT * FROM table t2 WHERE t2.value1 <= record.value1 AND t2.value2 <= 
record.value2 AND t3.value3 <= record.value3;