MySQL数据库错误,'where子句'中的未知列X

时间:2017-05-18 03:36:20

标签: php mysql join

我需要帮助修复数据库错误。我是MySQL新手。我收到这样的错误:

  

数据库错误:无效的SQL:CREATE TABLE gl2preethyd1495077842 AS   选择chart_id,sum(a.amount)为dr,sum(a.amount)为cr FROM   acc_trans a join gl b left outer join ar c ON a.trans_id = c.id WHERE if   (c.service_completion为null,(a.transdate< =''和a.transdate> =   ''),(c.service_completion< =''和c.service_completion> =''))   和a.trans_id = b.id GROUP BY chart_id ORDER BY chart_id       MySQL错误:1054('where子句'中的未知列'c.service_completion')

会话停止了。

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" ;

我无法在代码中找到问题。

1 个答案:

答案 0 :(得分:2)

错误是不言自明的:

  

错误:1054(未知栏&#39; c.service_completion&#39; in&#39; where子句&#39;)

检查以下一行:

$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 ";

此处c.service_completionar表中不存在的列(因为c是ar表的别名)。