子查询

时间:2015-06-25 21:36:58

标签: php sql sql-server sql-server-2008 subquery

我在MSSql中有2个表

表-A:

data|id_1|timeInIntForm
------------------------------
data1 | 1/22323/3 | 1433721600
data2 | 1/22323/3 | 1433721660

我有另一张桌子

表-B

data|(string list of ids)|startTimeinIntform|EndTimeInIntForm
--------------------------------------------------------------
dataA| (a_1223233_z a_1223233_x) | 1433721601 | 1433721659
datab| (a_1223233_z a_1223233_x) | 1433721602 | 1433721645

我想做一个

select * from table_a where id_1 = 'someId' 
and timeInIntForm between 'time1' and 'time2' 
and not between TableB_row1[startTimeinIntform] and TableB_row1[EndTimeInIntForm]'
.
.
.
and not between TableB_rowN[startTimeinIntform] and TableB_rowN[EndTimeInIntForm]'

ID不匹配,但可以在具有

的其他表上进行关联

table_c:

data|id_1|id_2
-----------------------------
data| 1/22323/3 | a_1223233_z
data| 1/22323/4 | a_1223233_x

我最初的想法是在PHP中从table_b获取行,然后当我从table_a中选择时,从table_b中的每一行构建not between子句。但如果可能的话,我愿意在一个选择声明中这样做。有任何想法吗?

我编辑了它以获得我要求的一些“样本”数据。我很难理解如何加入这些...我自愿承认我不是一个职业甚至是加盟的穷人,但我真的很难于如何将表b加入到a和c

1 个答案:

答案 0 :(得分:0)

嗯,我不确定没有桌子,但我认为这样做。

Select a.*
From table_a a
join table_c c
on a.ID_1 = c.ID_1
join table_b b
on c.ID_2 = b.ID_2
Where a.ID_1 = 'someid'
and a.timeinintform not between 'time1' and 'time2'
and a.timeinintform not between b.starttimeininform and b.endtimeinintform