SELECT 'V' as Ind, main.Voucher_No, main.Voucher_Prefix, 'SALESVOUCHER' as Voucher_Type, main.Voucher_Date as Date, concat(main.Voucher_No ,'-', main.Voucher_Prefix) as 'Voucher No', (SELECT Ledger_Name from $master_db.Ledger_Master where Ledger_ID=main.Ledger_ID) as Ledger,det.quantity as Quantity, det.SubTotal as 'Total Amount' From $trans_db.Sales_invoice_Main main JOIN Sales_Invoice_Details det ON main.voucher_no = det.voucher_no and main.voucher_prefix = det.voucher_prefix WHERE det.inventory_item_id = '$f1' AND (str_to_Date(concat(main.voucher_date, substr(timestamp, 11,19)) ,'%Y-%m-%d %H:%i:%s') >= str_to_date('$fromDateTime','%Y-%m-%d %H:%i:%s') and str_to_Date(concat(main.voucher_date, substr(timestamp, 11,19)) ,'%Y-%m-%d %H:%i:%s') <=str_to_date('$toDateTime','%Y-%m-%d %H:%i:%s') ) order BY cast(main.Voucher_No as unsigned) desc 