【问题标题】:CodeIgniter Active Record unknown column in where clauseCodeIgniter Active Record where 子句中的未知列
【发布时间】:2015-07-31 04:38:06
【问题描述】:

这是我的活动记录查询!它不断给我错误 where 子句中的未知列:

public function getItemSale($start, $end = 9,$userid,$loc_id,$item)
{
    $sql = "SELECT Name, sum(Qty) as total, sum(Price) as sale  from itransfile where 
    SESSIONID = GETSESSIONID(?) && PayMode IN('CASH','Cheque')" ;

     if(is_numeric($loc_id))
        $sql .= " && location_id = " .$loc_id ;
     else
        $sql .= " && location_id IN(SELECT location_id FROM client_locations where client_id = " .$userid. ")";

     if($item != 'All')
        $sql .= " && Name = `{$item}`"; 

     $sql .= " group by Name  order by sale desc LIMIT ? OFFSET ?;"; 

     $query = $this->db->query($sql, array(date("Y-m-d"),$end,(int)$start));

     return $query->result_array();
}

【问题讨论】:

  • 你的桌子是什么样子的?

标签: php codeigniter activerecord


【解决方案1】:

删除backticks 并使用AND 代替&&

 $sql = "SELECT Name, sum(Qty) as total, sum(Price) as sale  from itransfile where 
        SESSIONID = GETSESSIONID(?) AND PayMode IN('CASH','Cheque')";

if (is_numeric($loc_id))
    $sql .= " AND location_id = " . $loc_id;
else
    $sql .= " AND location_id IN(SELECT location_id FROM client_locations where client_id = " . $userid . ")";

if ($item != 'All')
    $sql .= " AND  Name = '{$item}'";

$sql .= " group by Name  order by sale desc LIMIT ? OFFSET ?;";

$query = $this->db->query($sql, array(date("Y-m-d"), $end, (int) $start));

return $query->result_array();

【讨论】:

  • 完成但问题仍未解决,它给出错误您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 2 行的“1 group by Name order by sale desc”附近使用 SELECT Name, sum(Qty) as total, sum(Price) as sale from itransfile where SESSIONID = GETSESSIONID('2015-05-16') && PayMode IN('CASH','Cheque') && location_id = 1 && Name = COMBO 1 group by Name order by sale desc;
  • 我认为未知列的问题已经解决。我认为这是新问题
  • @Mateen-ul-haq 我认为 COMBO 1 应该用单引号或双引号括起来,例如 "COMBO 1"
  • @saty no 如果项目中没有空格,它仍然给出相同的错误
  • SELECT Name, sum(Qty) as total, sum(Price) as sale from itransfile where SESSIONID = GETSESSIONID('2015-05-16') AND PayMode IN('CASH','Cheque' ) AND location_id = 1 && Name = 'COMBO 1' group by Name order by sale desc 这工作正常
【解决方案2】:

替换下一行

if($item != 'All')
        $sql .= " && Name = `{$item}`"; 

与

if($item != 'All')
            $sql .= " && Name = '{$item}'"; 

【讨论】:

    【解决方案3】:

    将反引号替换为单引号。在 MySql 中,反引号表示一个标识符是一个列名:

    $sql .= " && Name = '{$item}'"; 
    

    【讨论】:

    • 已完成但问题未解决,“where 子句”SELECT 名称中的未知列“Abc”,总和(数量)作为总计,总和(价格)作为 itransfile 的销售,其中 SESSIONID = GETSESSIONID('2015- 05-16') && PayMode IN('CASH','Cheque') && location_id IN(SELECT location_id FROM client_locations where client_id = 1) && Name = Abc group by Name order by sale desc;
    猜你喜欢
    • 2015-09-17
    • 2017-11-30
    • 1970-01-01
    • 2016-02-25
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    相关资源
    最近更新 更多