有一種使用cakephp使用agregate函數的方法?像sum()或avg()與find()方法。如何在CakePHP中使用agregate Mysql函數
UPDATE:
我錯過了線的書
array('fields'=>array('Product.type','MIN(Product.price) as price'), 'group' => 'Product.type');
顯示的基本結構做這件事。
感謝您的幫助
有一種使用cakephp使用agregate函數的方法?像sum()或avg()與find()方法。如何在CakePHP中使用agregate Mysql函數
UPDATE:
我錯過了線的書
array('fields'=>array('Product.type','MIN(Product.price) as price'), 'group' => 'Product.type');
顯示的基本結構做這件事。
感謝您的幫助
在find方法調用的fields參數中,您可以傳遞由聚合函數處理的字段。例如:
$Model->find('all',
array(
'anything' => array(/* */),
'fields' => array(
'SUM (Model.attribute) AS total',
'OTHERFUNCTION(OModel.other_attribute) AS other'
),
'otherthing' => array(/* */)
)
);
在完全相同的方式,從CakePHP只是一個PHP框架。
<?php
// Make a MySQL Connection
$query = "SELECT type, SUM(price) FROM products GROUP BY type";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "Total ". $row['type']. " = $". $row['SUM(price)'];
echo "<br />";
}
?>
感謝您的回答,是的,我知道你可以直接發送查詢到MySQL,但我想知道是否有一種方法,蛋糕管理這種查詢使用find($ type,$ params)方法,並使所有的魔術事物,而不是使用查詢(字符串$查詢)。並手動構建查詢。 – omabena 2011-04-08 03:22:13
如果你不知道cakephp爲什麼打擾 – dogmatic69 2011-04-08 08:56:11
是的,我終於讓它工作:),感謝您的幫助,我只需要添加該組使用該功能。 – omabena 2011-04-08 04:15:09
如果你想改善,請查看http://book.cakephp.org/view/1608/Virtual-fields – dogmatic69 2011-04-08 08:56:50
感謝您的幫助,現在我想知道是否沒有限制使用它與可控行爲? – omabena 2011-04-08 12:54:19