左边加入哪里和喜欢

时间:2016-10-06 08:13:31

标签: php mysql

我有这个查询我使用PHP搜索代码问题是在localhost这个查询工作,并给我我需要的结果但在线我收到此消息

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近'其中tbl_project.db_projectname喜欢'%saida%'要么   tbl_project.db_location lik'在第30行

php code

 $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."%' ";
    } 
$second=true;
   foreach($qq as $qu){  
        if($second){  
        $sq .= " where  ".$qu;      
        $second = false;  
        }else{$sq .= " or ".$qu;}} 
$sql=mysqli_query($conn,"SELECT
    tbl_project.db_id,
    tbl_project.db_projectname,
    tbl_project.db_location,
    tbl_project.db_client,
    tbl_project.db_transferredto,
    tbl_project.db_psd,
    tbl_project.db_pdd,
    tbl_project.db_duration,
    tbl_project.db_past,
    tbl_project.db_padd,
    tbl_project.db_aduration,
    tbl_project.db_percent,
    tbl_project.db_pnote,
    tbl_project.db_user,
    tbl_project.db_cpercentage,
    tbl_project.db_epercentage,
    tbl_project.db_mpercentage,
    tbl_project.db_status,
    tbl_project.db_offer,
    tbl_project.db_sheet,
    tbl_project.db_invoice,
    tbl_project.db_po,
    sum(
        tbl_activities.db_totalcost
    ) AS total_cost
FROM
    tbl_project
LEFT JOIN tbl_activities ON tbl_project.db_projectname = tbl_activities.db_projectname
{$sq} GROUP BY tbl_project.db_id")or die(mysqli_error($conn));

/ ********************************

SELECT tbl_project.db_id, tbl_project.db_projectname, tbl_project.db_location, tbl_project.db_client, tbl_project.db_transferredto, tbl_project.db_psd, tbl_project.db_pdd, tbl_project.db_duration, tbl_project.db_past, tbl_project.db_padd, tbl_project.db_aduration, tbl_project.db_percent, tbl_project.db_pnote, tbl_project.db_user, tbl_project.db_cpercentage, tbl_project.db_epercentage, tbl_project.db_mpercentage, tbl_project.db_status, tbl_project.db_offer, tbl_project.db_sheet, tbl_project.db_invoice, tbl_project.db_po, sum( tbl_activities.db_totalcost ) AS total_cost FROM tbl_project LEFT JOIN tbl_activities ON tbl_project.db_projectname LIKE tbl_activities.db_projectname where tbl_project.db_id='79' where tbl_project.db_projectname like '%saida%' or tbl_project.db_location like '%saida%' or tbl_project.db_client like '%saida%' or tbl_project.db_offer like '%saida%' or tbl_project.db_sheet like '%saida%' or tbl_project.db_invoice like '%saida%' or tbl_project.db_po like '%saida%' GROUP BY tbl_project.db_id

2 个答案:

答案 0 :(得分:0)

您添加了两个“where”子句。

SELECT tbl_project.db_id, tbl_project.db_projectname, 
tbl_project.db_location, tbl_project.db_client, 
tbl_project.db_transferredto, 
tbl_project.db_psd, tbl_project.db_pdd, tbl_project.db_duration, 
tbl_project.db_past, tbl_project.db_padd, tbl_project.db_aduration, 
tbl_project.db_percent, tbl_project.db_pnote, tbl_project.db_user, 
tbl_project.db_cpercentage, tbl_project.db_epercentage, 
tbl_project.db_mpercentage, tbl_project.db_status, tbl_project.db_offer,
tbl_project.db_sheet, tbl_project.db_invoice, tbl_project.db_po, 
sum( tbl_activities.db_totalcost ) AS total_cost FROM tbl_project
LEFT JOIN tbl_activities ON tbl_project.db_projectname 
LIKE tbl_activities.db_projectname where tbl_project.db_id='79'
and tbl_project.db_projectname like '%saida%' or    
tbl_project.db_location 
like '%saida%' or tbl_project.db_client like '%saida%' or 
tbl_project.db_offer 
like '%saida%' or tbl_project.db_sheet like '%saida%' or 
tbl_project.db_invoice 
like '%saida%' or tbl_project.db_po like '%saida%' GROUP BY  
tbl_project.db_id

答案 1 :(得分:-2)

在左连接部分使用“LIKE”而不是“=”,因此它将是

LEFT JOIN tbl_activities ON tbl_project.db_projectname LIKE tbl_activities.db_projectname
{$sq} GROUP BY tbl_project.db_id")or die(mysqli_error($conn));