Mysql 如何用cakephp编写SQL查询?
用cakephp语法转换成银色?试试这个Mysql 如何用cakephp编写SQL查询?,mysql,cakephp,converter,Mysql,Cakephp,Converter,用cakephp语法转换成银色?试试这个 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
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')
您可以通过以下步骤将原始SQL修改为CakePHP格式 或者出于任何原因,您不能/不会使用原始SQL查询,如下所示: 在控制器中: 在模型中:
为了供其他人参考,如果我们转换成CakePHP方法,这个查询将是这样的
$this->query('SELECT * FROM table');
相信我,它不会阻止SQL注入(@Vineet您一定是在谈论原始查询,对吧?因为当使用CakePHP制定查询时,它会净化等以防止SQL注入。另外,对于原始查询,请参阅“是”处的“准备语句”。@Vineet很高兴知道,这个讨论也将帮助未来的读者。这对我很有帮助
$this->ModelName->query('SELECT * FROM table');
$this->query('SELECT * FROM table');
$result = $this->ModelName->find("all",array(
'fields' => array('campaign','lead_status','count(id') as counts),
'conditions' => array(
'lead_status' => array('Very Hot','Hot', 'Warm', 'Cold', 'Not Contacted'),
'campaign' => array('stage1','stage2','stage3','stage4'),
"created_on >='2012-10-01' ",
'is_put_for_exchange' => 0,
'transaction_type' => 'First Sale',
'propertytype' => 'Residential'
),
'group' => array('Buyer.campaign','Buyer.lead_status'),
'order' => 'Buyer.campaign'
));