2015-11-18 78 views
2
$query = "select (select count(a.serial_no) from tra2 a where a.model_no = ".$id." and a.flag = cast(3 as character varying) + (select count(a.serial_no) from stk a where a.model_no = ".$id." and a.trans_id is NULL)as qty)"; 
    $result = $this->db->query($query); 
    return $result->result(); 

當我運行查詢這個查詢有什麼錯誤?

ERROR: syntax error at or near "as" 
LINE 1: ...ck a where a.model_no = K258 and a.trans_id is NULL)as qty) 

回答

2

追求的一端必須是:

...a.trans_id is NULL)) as qty" 

目前你有一些像這樣的

select (select 1 + 2 as qty) 

但你要

select (select 1 + 2) as qty 
4

WHERE條款加入''(引號)。我猜的數據類型爲VARCHAR所以你必須在查詢中使用引號

喜歡這個 .... WHERE a.model_no = '" . $id . "' AND ...