MySQL 如何在 cakephp 中编写 SQL 查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22497923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to write SQL Query in cakephp?
提问by Vikas Kale
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 Syntax plz It Argent?
转换为 cakephp 语法 plz It Argent?
回答by
You can modify your raw SQL into CakePHP format by going through the documentation.
您可以通过阅读文档将原始 SQL 修改为 CakePHP 格式。
Or for any reason you cannot / will not, then you can use the raw SQL query as follows:
或者出于任何原因您不能/不会,那么您可以使用原始 SQL 查询,如下所示:
In controller:
在控制器中:
$this->ModelName->query('SELECT * FROM table');
In model:
在模型中:
$this->query('SELECT * FROM table');
回答by Sadikhasan
Try this
尝试这个
$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'
));
回答by Sanjun Dev
For reference to others, this query will be look like this if we convert into CakePHP method
供其他人参考,如果我们转换为 CakePHP 方法,这个查询将是这样的
$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'
));