2014-02-11 233 views
0

我有一個登錄查詢,我想運行兩個條件。基本上,在查詢號碼1中,我檢查會員是否連接到我的系統中的任何合約,並在查詢2中檢查會員是否擁有我係統中的任何單位。返回兩個查詢的結果

我想設置一個查詢,它同時運行,如果返回結果(m。*),那麼應該返回結果。

我還沒有低於我想兩個查詢做出一個出來的:

select m.* FROM member m 
inner join unit_contract_member ucm on ucm.member_id = m.id 
inner join unit_contract uc on uc.id = ucm.contract_id 
inner join unit u on u.id = uc.unit_id 
where m.usr = '[email protected]' 
and m.pwd = 'test' 
and u.community_id = 1 
and m.active = true 
and uc.active = true; 

select m.* from member m 
inner join unit_owner uo on uo.member_id = m.id 
inner join unit u on u.id = uo.unit_id 
where m.usr = '[email protected]' 
and m.pwd = 'test' 
and u.community_id = 1 
and m.active = true; 

誰能幫助這嗎?將不勝感激。

問候,

鮑勃

回答

0

兩個表在使用UNION ALL的概念,它會顯示所有記錄的字段相同的意思。

+0

對不起,對UNION ALL不太熟悉,你能舉個例子嗎? –

+0

谷歌搜索,它工作正常!謝謝! –

0
IF EXISTS(select m.* FROM member m 
     inner join unit_contract_member ucm on ucm.member_id = m.id 
     inner join unit_contract uc on uc.id = ucm.contract_id 
     inner join unit u on u.id = uc.unit_id 
     where m.usr = '[email protected]' 
     and m.pwd = 'test' 
     and u.community_id = 1 
     and m.active = true 
     and uc.active = true) 
     SELECT m.* FROM member m 
     inner join unit_contract_member ucm on ucm.member_id = m.id 
     inner join unit_contract uc on uc.id = ucm.contract_id 
     inner join unit u on u.id = uc.unit_id 
     where m.usr = '[email protected]' 
     and m.pwd = 'test' 
     and u.community_id = 1 
     and m.active = true 
     and uc.active = true 
ELSE 
     select m.* from member m 
     inner join unit_owner uo on uo.member_id = m.id 
     inner join unit u on u.id = uo.unit_id 
     where m.usr = '[email protected]' 
     and m.pwd = 'test' 
     and u.community_id = 1 
     and m.active = true; 
+0

嗨,即時獲取意外的IF錯誤? –