Shiva Prakashan
Shiva Prakashan

Reputation: 109

What mistakes with this query?

$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();

When i run the query

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

Upvotes: 2

Views: 50

Answers (2)

Tomasz Jakub Rup
Tomasz Jakub Rup

Reputation: 10680

The end of the quest must be:

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

Currently You have some like this

select (select 1 + 2 as qty)

but You want

select (select 1 + 2) as qty

Upvotes: 2

Gilang
Gilang

Reputation: 311

Add '' (quotes) on your WHERE clauses. I guess the data type is VARCHAR so you have to use quotes in your query

Like this .... WHERE a.model_no = '" . $id . "' AND ...

Upvotes: 4

Related Questions