MySql Database Error Issue : I need help for fixing database error. I am getting an error like this:
    I am new to mysql.
    *****************************************************************************
     Database error: Invalid SQL: CREATE TABLE gl2preethyd1495077842 AS SELECT chart_id, sum(a.amount) as dr, sum(a.amount) as cr FROM acc_trans a join gl b left outer join ar c ON a.trans_id=c.id WHERE if ( c.service_completion is null, ( a.transdate <='' and a.transdate >= '' ), ( c.service_completion <='' and c.service_completion >= '' ) ) and a.trans_id=b.id GROUP BY chart_id ORDER BY chart_id
    MySQL Error: 1054 (Unknown column 'c.service_completion' in 'where clause')
    
    Session halted.	
    
    ******************************************************************************
    
    CODE:
    
    $sql =  "CREATE TABLE $temp2 AS " ;
    
    $sql.=  "SELECT chart_id, sum(a.amount)  as dr, sum(a.amount) as cr FROM acc_trans a join gl b left outer join ar c ON a.trans_id=c.id " ;
    
    $sql .="WHERE if ( c.service_completion is null, ( a.transdate <='$TB_ToDate' and a.transdate >= '$TB_FromDate' ), ( c.service_completion <='$TB_ToDate' and c.service_completion >= '$TB_FromDate' ) ) and a.trans_id=b.id ";
    
    if ($attrib1<>'')       $sql .= " and attrib1=$attrib1 " ;
    if ($attrib2<>'')       $sql .= " and attrib2=$attrib2 " ;
    if ($attrib3<>'')       $sql .= " and attrib3=$attrib3 " ;
    $sql.=  "GROUP BY chart_id ORDER BY chart_id" ;
    
    Can you guys help me to fix the issue ASAP? I am unable to find where the issue is.

