简体   繁体   中英

search from first table sum from second table

I have a form for search the user can enter any letter word and do his search this search is in two tables table 1 is tbl_project this table contain

db_projectname db_location db_client  
xxxx              vvv      ccc

dddd              ggg      tttt

tbl_activities contain

db_projectname  db_cost  db_name   
xxxx             500     ttt
xxxx             500     hhh
dddd             200     llll

i use this php code to do my search

$sq = ""; 
$qq=array();
    if(isset($_POST['search']) && !empty($_POST['search'])){  
    $search =  mysqli_real_escape_string($conn,$_POST['search']); 
    $qq[] = "tbl_project.db_projectname like '%".$search."%' ";  
    $qq[] = "tbl_project.db_location like '%".$search."%' ";
    $qq[] = "tbl_project.db_client like '%".$search."%' ";
    $qq[] = "tbl_project.db_offer like '%".$search."%' ";    
    $qq[] = "tbl_project.db_sheet like '%".$search."%' ";
    $qq[] = "tbl_project.db_invoice like '%".$search."%' ";
    $qq[] = "tbl_project.db_po like '%".$search."%' ";
    $qq[] = "tbl_project.db_id like '%".$search."%' ";    
    } 
$second=true;
       foreach($qq as $que){  
        if($second){  
        $sq .= " where ".$que;      
        $second = false;  
        }else{  
        $sq .= " or ".$que;          
        }} 

the result should be like this if the user enter x or v or c or xxxx or vvv or ccc

xxxx 1000 vvv cccc

how should the query be to have this result i have bee tried different query but i didn't receive this result

SELECT  tbl_project.* 
      , (SELECT  sum(tbl_activities.db_totalcost) AS total_cost FROM tbl_activities {$sq}
FROM tbl_project

Try this approach:

$cond = "";
if(isset($_POST['search']) && !empty($_POST['search'])){  
    $search =  mysqli_real_escape_string($conn,$_POST['search']); 
    $cond .= " and pa.db_projectname like '%".$search."%' ";  
    $cond .= " and pa.db_location like '%".$search."%' ";
    $cond .= " and pa.db_client like '%".$search."%' ";
    $cond .= " and pa.db_offer like '%".$search."%' ";    
    $cond .= " and pa.db_sheet like '%".$search."%' ";
    $cond .= " and pa.db_invoice like '%".$search."%' ";
    $cond .= " and pa.db_po like '%".$search."%' ";
    $cond .= " and pa.db_id like '%".$search."%' ";    
} 

And Query will be:

select p.*,sum(pa.db_totalcost) from tbl_project p left join 
tbl_activities pa on p.db_projectname=pa.db_projectname 
where 1 $cond group by p.db_projectname

I think you want something like that:

SELECT p.db_projectname, IFNULL(a.total, 0) as total, p.db_location, p.db_client FROM tbl_project as p
LEFT JOIN (select db_projectname, sum(db_cost) as total from tbl_activities group by db_projectname) as a
    ON p.db_projectname = a.db_projectname

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM