如何基于两个条件以不同的方式从两个表中获取数据,而不使用联合

时间:2019-07-29 07:11:20

标签: mysql sql database oracle

我有两个表reconciliation_datainventory_transaction。 两个表都有event_idstatususer_idinventory_transaction.idinventory_transaction_id中作为reconciliation_data的外键。现在,我已根据以下两个条件从两个表中获取数据

1) Join both tables if inventory_transaction_id in reconciliation_data is not null. 
2) If status in reconciliation_data is 'NEW_FOUND', then inventory_transaction_id in reconciliation_data will be null, fetch the data from only reconciliation_data for that particular event_id and user_id. 

我希望有一个没有联合的查询,可以达到目的。

我尝试使用联合,但是我们不能使用联合,因为out框架不支持联合。

select r.id, it.status, r.physical_inventory_status, it.asset_id, r.inventory_id, r.event_id, r.sfl_user_id 
from rbxdev.inventory_transaction it 
left join rbxdev.reconciliation_data r on r.inventory_transaction_id = it.id and r.sfl_user_id = 3  
where it.event_id = 29
union 
select r1.id, r1.physical_inventory_status, r1.physical_inventory_status, r1.inventory_id, r1.inventory_id, r1.event_id, r1.sfl_user_id 
from rbxdev.reconciliation_data r1 
where r1.physical_inventory_status = 'NEW_FOUND' and r1.event_id = 29 and r1.sfl_user_id = 3;

期望的是没有联合的单个查询,可以解决此问题。

0 个答案:

没有答案