[英]sql query applying multiple filters to multiple tables
我對SQL沒有經驗,需要一個查詢將多層過濾器應用於多個表以進行比較。
表格1:
訂單編號步驟名稱數據參數名稱
12 step4 const1 P1
12 step4 const2 P2
12 step4 value1 P3
30步驟6 const3 P1
30步驟6 const4 P2
30 step6 value2 P3
所有表都具有相同的格式,只是數據值不同。 表格之間的訂單號可能會有所不同。
我想要做:
訂單編號表數據參數名稱
12表1值1 P3
12表2值2 P3
14表3值3 P3
非常感謝您的幫助!
您需要添加union all...select
根據需要union all...select
零件:
select
order_number,
'table1' as `table`,
data,
prameter_name
from
table1 t1p3
where
step_name in ('step1', 'step2', 'step3', 'step4', 'step5', 'step6', 'step7', 'step8') and
parameter = 'p3' and
exists (
select
'x'
from
table1 t1p1
where
t1p1.order_number = t1p3.order_number and
t1p1.step_name = t1p3.step_name and
t1p1.parameter = 'p1' and
t1p1.data = 'const1'
) and exists (
select
'x'
from
table1 t1p2
where
t1p2.order_number = t1p3.order_number and
t1p2.step_name = t1p3.step_name and
t1p2.parameter = 'p2' and
t1p2.data = 'const2'
)
union all
select
order_number,
'table2',
data,
prameter_name
from
table2 t2p3
where
step_name in ('step1', 'step2', 'step3', 'step4', 'step5', 'step6', 'step7', 'step8') and
parameter = 'p3' and
exists (
select
'x'
from
table2 t2p1
where
t2p1.order_number = t2p3.order_number and
t2p1.step_name = t2p3.step_name and
t2p1.parameter = 'p1' and
t2p1.data = 'const1'
) and exists (
select
'x'
from
table2 t2p2
where
t2p2.order_number = t2p3.order_number and
t2p2.step_name = t2p3.step_name and
t2p2.parameter = 'p2' and
t2p2.data = 'const2'
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.