mysql - How do i join 3 different types of sql queries into 1 -
i have 1 main query , based on it's results have create 3 more in order display data need. loop, however, creates many queries server. understand it's possible avoid sql joins, i'm not how work in case.
select * `newloads` `id` '%".$term."%' or `loadnumber` '%".$term."%' order `pudate` desc //this main query , every result produces query following in **while** loop: select count(*) `newstops` idload = '".$load['id']."' select newstops.idconsignee, consignees.name, consignees.address `newstops` inner join `consignees` on newstops.idconsignee = consignees.id idload = '".$load['id']."' order stopnumber asc limit 1 //the last 1 same previous it's ordered stopnumber desc select newstops.idconsignee, consignees.name, consignees.address `newstops` inner join `consignees` on newstops.idconsignee = consignees.id idload = '".$load['id']."' order stopnumber desc limit 1
right i'm combining results in json output , need keep same way
this query not debugged enough:
select t1.*, count(t2.*) newstops_count, t3.name, t3.address, max(t3.stopnumber), min(t3.stopnumber) `newloads` t1 left join `newstops` t2 on t2.idload = t1.id inner join `consignees` t3 on t2.idconsignee = t3.id `id` '%".$term."%' or `loadnumber` '%".$term."%' order `pudate` desc
so if can provide sqlfiddle data lot.
Comments
Post a Comment