SHOW CREATE TABLE scm_issue_hdr ; https://www.educba.com/mysql-string-functions/ select issue_no,store_receipt_no `SR NO`,issue_date `Issue Date `,hdr_id `Issue No`,machine_id `Machine No`,mechine_name `Machine Name`,dept_desc `Department`, itemcode `Item Code`,item_desc `Description `,issue_qty `Quantity`,uom_code `Unit`, issue_value `Issue Value`, stock `Stock Quantity` , indent_type_desc `Expenses type `,cost_desc `Cost Factor` from ( select c.*,d.cost_desc from ( select a.*,mm.mechine_name from ( select issue_no,store_receipt_no,issue_date,hdr_id,md.dept_desc, concat(sih.group_code,sih.item_code) itemcode,i.item_desc,issue_qty,sih.uom_code, sih.issue_value,sitm.indent_type_desc,sih.machine_id,stock,sih.company_id,tpp.name Project,sih.deptcost, sih.internal_note from scm_issue_hdr sih,itemmaster i,tbl_proc_inward tpi,master_department md,scm_indent_type_master sitm,tbl_proc_project tpp where issue_date>='$fromdate' and issue_date<='$todate' and sih.item_id=i.item_id and sih.inward=tpi.inward_id and sih.dept_id=md.mdept_id and sih.indent_type_id=sitm.indent_type_code and sih.company_id=$cmpid and sih.project_id=tpp.project_id ) a left join mechine_master mm on a.machine_id=mm.mechine_id ) c left join costmaster d on c.deptcost=d.id and c.company_id=d.company_id ) h order by hdr_id,issue_no