Thank you @kick and @miko.
I am reading up on some SQL JOIN to see if there is anything I can do.
I had already done something like what @kick sugested for the time being, but it isn't the correct solution. This is what I had so far. Let me know if what I have is better than what @kick sugested in terms of performance.
select Transaction.trans_Id, Transaction.dinheiro, Transaction.ficha, Transaction.transaction_dt as data_inserido, b.numBanco, c.numTracker
from Transaction inner join (select t.trans_Id, count(t.trans_Id) as numBanco
from Transaction t inner join Transaction_Banco as tb on t.trans_Id = tb.trans_id
where t.transaction_type_id = 1 and t.usuario_id = $usuarioId group by t.trans_Id) as b ON Transaction.trans_Id = b.trans_id
inner join (select t.trans_Id, count(t.trans_Id) as numTracker
from Transaction t inner join Transaction_Tracker tt on t.trans_Id = tt.trans_id
where t.transaction_type_id = 1 and t.usuario_id = $usuarioId group by t.trans_Id) as c ON Transaction.trans_Id = c.trans_id
where transaction_type_id = 1 and usuario_id = 1
group by Transaction.trans_Id, dinheiro, ficha, cotacao, data_inserido;
In regards to my problem I am thinking of doing the following:
select * from (select 1 as querytype, a.trans_id, a.dinheiro, a.ficha, a.transaction_dt, null as banco_id, null as tracker_id
from Transaction as a where a.usuario_id = 1 and transaction_type_id = 1
union
select 2 as querytype, a.trans_id, b.dinheiro, null as ficha, b.trans_banco_dt, b.banco_id, null as tracker_id
from Transaction as a INNER JOIN Transaction_Banco as b ON a.trans_id = b.trans_id
where a.usuario_id = 1 and transaction_type_id = 1
union
select 3 as querytype, a.trans_id, null as dinheiro, b.ficha, b.trans_carteira_dt, null as banco_id, b.tracker_id
from Transaction as a Inner join Transaction_Tracker as b ON a.trans_id = b.trans_id
Where a.usuario_id = 1 and transaction_type_id = 1) as a order by trans_id, querytype;
Using this query I can use @miko's solution while looping through the dataset. Basically I will print each section based on the query type.
I think the problem is solved. Just give me some feedback about the speed comparison between my query and @kick's query.
@miko, what do you think?