>ok i have 3 table in my databases. TB_ACCOUNT(containt all the customer account),TB_INVOICE(containt all the customer invoices),TB_SALESMAN(containt all my salesman). In the tb_account in a field name tr_idsalemans. and in the invoice i have a field name tr_idaccount. so i want to be able when i select a salesment to have all of the account that he have and have the last invoice amount that the customer ordered. it is possible to make it in one sql statement or more. how can i do it thanks
Try
select S.*, AC.*, In.* from Tb_Salesman S LEFT JOIN TB_Account Ac on S.tr_idsalemans = Ac.tr_idsalemans
LEFT JOIN
(select tr_idaccount, Amount from TB_INVOICE Inv1
INNER JOIN (select tr_idaccount, max(Inv_Date) as MaxInDate) MaxDates ON
Inv1.tr_idaccount = MaxDates.tr_idaccount and Inv1.Date = MaxDates.MaxInDate) In
on Ac.tr_idaccount =In.tr_idaccount
If it's not broken, fix it until it is.
My Blog