2014-04-07 70 views
0

我有一個SQL查詢的問題。這是原來的功能:PHP:爲什麼我不能在這個SQL查詢中使用WHERE?

function selectAllSorted($active) 
{ 
    return $this->selectObjects("SELECT bp.*, p.title as product_title 
           FROM $this->_table bp 
           INNER JOIN ?_product p USING (product_id) 
           ORDER BY 0+sort_order,p.title"); 
} 

,我修改了它看起來像這樣:

function selectAllSorted($active) 
{ 
    return $this->selectObjects("SELECT bp.*, p.title as product_title 
           FROM $this->_table bp 
           WHERE product_id =$active 
           INNER JOIN ?_product p USING (product_id) 
           ORDER BY 0+sort_order,p.title"); 
} 

,但我得到一個未知的錯誤,因爲我看不到日誌我不能找出它在哪裏,或者爲什麼在這裏使用WHERE是錯誤的。

這是函數selectObjects是如何定義的:

function selectObjects($sql, $param1 = null) 
    { 
     $args = func_get_args(); 
     $q = call_user_func_array(array($this->_db, 'queryResultOnly'), $args); 
     $ret = array(); 
     while ($row = $this->_db->fetchRow($q)) 
     { 
      $obj = new $this->_recordClass($this); 
      $obj->fromRow($row); 
      $ret[] = $obj; 
     } 
     return $ret; 
    } 

你們可以搞清楚什麼是錯?

謝謝。

+0

移動'WHERE'條款,並將其放在'JOIN' –

+0

感謝您的後也會搖擺。 –

回答

3

你的語法是正確的 - where條款來了join條款(S)後:

function selectAllSorted($active) 
{ 
    return $this->selectObjects("SELECT bp.*, p.title as product_title 
           FROM $this->_table bp 
           INNER JOIN ?_product p USING (product_id) 
           WHERE bp.product_id =$active 
           ORDER BY 0+sort_order,p.title"); 
} 
+0

它可能會需要從表中選擇一個的product_id,即WHERE'= p.productid ...',不是嗎? –

+0

@TomasPastircak你好,趕上!在我的答案中添加了'bp'前綴。 – Mureinik

+0

謝謝!這很快!我雖然命令沒有關係。 –

0

連接查詢了Syntex是:

Select t1.* from table1 t1 
inner join table2 t2 on t1.col1=t2.col1 
where t1.col2> 0 and t2.col3>4 

現在,您的查詢的樣子:

function selectAllSorted($active) 
{ 
    return $this->selectObjects("SELECT bp.*, p.title as product_title 
           FROM $this->_table bp 
           INNER JOIN ?_product p on pb.product_id=p.product_id 
           WHERE bp.product_id =$active         
           ORDER BY 0+sort_order,p.title"); 
} 
+0

謝謝,這也很有用。 –

相關問題