[英]sql where clause for each row of a subquery
我在MSSql中有2個表
table_a:
data|id_1|timeInIntForm
------------------------------
data1 | 1/22323/3 | 1433721600
data2 | 1/22323/3 | 1433721660
我還有一張桌子
table_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聯接而感到困惑
好吧,我不確定是否完全沒有桌子,但是我認為這可以做到。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.