2014-03-19 103 views
2
select campaign,lead_status,COUNT(id) 
from buyers 
where lead_status IN('Very Hot','Hot', 'Warm', 'Cold', 'Not Contacted') 
    and campaign IN('stage1','stage2','stage3','stage4') and created_on >='2012-10-01' 
    and is_put_for_exchange='0' and transaction_type='First Sale' 
    and propertytype='Residential' 
group by campaign,lead_status 
ORDER BY campaign asc, field(lead_status,'Very Hot','Hot', 'Warm', 'Cold', 'Not Contacted') 

convert in cakephp語法plz It Argent?如何在cakephp中編寫SQL查詢?

回答

7

您可以通過documentation去修改原始的SQL進入CakePHP的格式。

或因任何原因,你不能/不會,那麼你可以按如下方式使用原始的SQL查詢:

在控制器:

$this->ModelName->query('SELECT * FROM table'); 

在模型:

$this->query('SELECT * FROM table'); 

Documentation of query()

+0

相信我,它不會阻止SQL注入。 :( – Vineet

+0

@Vineet你一定是在談論原始查詢,對嗎?因爲當使用CakePHP制定查詢時,它會清理等,以防止SQL注入。另外,對於原始查詢,請參閱_prepared statements_ at http://book.cakephp.org /2.0/en/models/retrieving-your-data.html#prepared-statements –

+0

是的,我現在正在使用預先準備的語句。​​ – Vineet

6

試試這個

$this->Buyer->find('all', array(
'fields' => $arrayOfFields, 
'conditions' => array(
    'Buyer.lead_status' => $arrayOfLeadStatus, 
    'Buyer.compaign' => $arrayOfCompaign, 
    'Buyer.is_put_for_exchange' => 0, 
    'Buyer.transaction_type' => 'First Sale', 
    'Buyer.propertytype' => 'Residential' 
    ), 
'group' => array('Buyer.campaign','Buyer.lead_status'), 
'order' => 'Buyer.campaign' 
));