从Cakephp中的数据库中选择数据不起作用

时间:2013-12-03 02:35:07

标签: mysql sql cakephp

我想选择像这样的SQL查询的数据:

SELECT discounts.product_id, products.product_name,
sum(products.product_price - discounts.product_discount) as total_Amount,
count(orders.order_id) as total_Number
FROM products 
inner join discounts on products.product_id = discounts.product_id
inner join orders on discounts.discount_id = orders.discount_id

where discounts.start_time >= @from and discounts.end_time <= @to 

group by discounts.product_id,products.product_name

@from和@to是我将传递给的值。

以下是我在Controller中所做的:

$from='27 November 2012';
        //$this->layout = 'customer-backend';
        $this->Order->recursive=-1;
        $this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)';
        $this->Order->virtualFields['number']='COUNT(Order.order_id)';
        $option['joins'] = array(
            array('table'=>'discounts',
                'alias'=>'Discount',
                'type'=>'INNER',
                'conditions'=>array(
                    'Order.discount_id = Discount.discount_id',
                )
            ),
            array('table'=>'products',
                'alias'=>'Product',
                'type'=>'INNER',
                'conditions'=>array(
                    'Discount.product_id = Product.product_id'
                )
            )
        );
        $option['fields']= array('Discount.product_id','Product.product_name','benefit','number');
        $option['conditions']=array('Discount.start_time >='=>$from, 'Discount.end_time <= ' => $to);   //I guess it's wrong here
        $option['group'] = array('Discount.product_id','Product.product_name');
        //$option['limit']=20;
        $products = $this->Order->find('all',$option);
        $this->set('products',$products);

我认为这一行是错误的:

$option['conditions']=array('Discount.start_time >='=>$from, 'Discount.end_time <= ' => $to);

当我删除它时,它有效。但我不知道如何解决它。 请帮帮我。

1 个答案:

答案 0 :(得分:1)

不是CakePHP的问题,但他们的方式是MYSQL接收您的查询。尝试格式化日期。

$from = date('Y-m-d', strtotime($from));