是否可以生成此查询?

时间:2017-06-08 05:21:59

标签: sql join inner-join

Select * From table1
如果table1.field1=1则内部加入table2
否则如果table1.field1=2则加入内部加入table3

2 个答案:

答案 0 :(得分:2)

我认为你正在寻找类似的东西,即使你的问题并不完全清楚:

SELECT *
FROM table1 t1
LEFT JOIN table2 t2 ON <join condition> AND t1.field1 = 1
LEFT JOIN table3 t3 ON <join condition> AND t1.field1 = 2

答案 1 :(得分:1)

SELECT glbank.check_num
, glbank.ref_num
, glbank.check_amt
, glbank.check_date
, glbank.bank_code
, vendaddr.name
, vendor.vend_num 
, vendor.vend_remit
FROM ((aptrxp_all
INNER JOIN glbank 
ON aptrxp_all.check_num=glbank.check_num) 
INNER JOIN vendaddr 
ON aptrxp_all.vend_num=vendaddr.vend_num) 
INNER JOIN vendor
ON vendaddr.vend_num=vendor.vend_num
If vendaddr.vend_num=vendor.vend_num Then
(SELECT vendaddr.name
FROM vendaddr 
INNER JOIN vendor
ON vendaddr.vend_num=vendor.vend_num where vendaddr.vend_num=vendor.vend_num)
Else (SELECT vendaddr.name
FROM vendaddr 
INNER JOIN vendor
ON vendaddr.vend_num=vendor.vend_remit where 
vendaddr.vend_num=vendor.vend_remit)
end 
where glbank.check_amt= @amount