У меня есть 3 таблицы: orders
, discounts
и products
таким образом, что у product
много discounts
(скидка). Discount
имеет много orders
. другими словами, это выглядит так: Product
> Discount
> Order
.
Я хочу получить данные из 3 таблиц в качестве необработанного mySQL-запроса ниже:
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 group by discounts.product_id,products.product_name
Это то, что я сделал:
$this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)'; $this->Order->virtualFields['number']='COUNT(Order.order_id)'; $products = $this->Order->find('all',array('contain'=>array('Discount'=>array('Product'))), array( 'limit'=>20, 'fields'=>array('benefit','number'), 'group'=>array('Discount.product_id','Product.product_name'))); Debugger::dump($products); $this->set('products',$products);
-$this->Order->virtualFields['benefit']='SUM(Product.product_price - Discount.product_discount)'; $this->Order->virtualFields['number']='COUNT(Order.order_id)'; $products = $this->Order->find('all',array('contain'=>array('Discount'=>array('Product'))), array( 'limit'=>20, 'fields'=>array('benefit','number'), 'group'=>array('Discount.product_id','Product.product_name'))); Debugger::dump($products); $this->set('products',$products);
Но у меня есть ошибка:
Database Error Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Product.product_price' in 'field list' SQL Query: SELECT `Order`.`order_id`, `Order`.`user_id` `Order`.`order_date`, `Order`.`payment`, `Order`.`discount_id`, `Order`.`total`, (SUM(`Product`.`product_price` - `Discount`.`product_discount`)) AS `Order__benefit`, (COUNT(`Order`.`order_id`)) AS `Order__number`, `Discount`.`discount_id`, `Discount`.`product_id`, `Discount`.`product_discount`, `Discount`.`start_time`, `Discount`.`end_time` FROM `project`.`orders` AS `Order` LEFT JOIN `project`.`discounts` AS `Discount` ON (`Order`.`discount_id` = `Discount`.`discount_id`) WHERE 1 = 1
Кажется, что сдерживаемый не работал, так как он не содержал таблицу продуктов в запросе.
EDIT: согласно предложению Дейва, я использовал JOIN:
$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' ) ) ); $products = $this->Order->find('all',$option, array( 'limit'=>20, 'fields'=>array('Discount.product_id','Product.product_name'), 'group'=>array('Discount.product_id','Product.product_name'))); Debugger::dump($products); $this->set('products',$products);
-$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' ) ) ); $products = $this->Order->find('all',$option, array( 'limit'=>20, 'fields'=>array('Discount.product_id','Product.product_name'), 'group'=>array('Discount.product_id','Product.product_name'))); Debugger::dump($products); $this->set('products',$products);
Однако, какие $ продукты содержат только:
array( (int) 0 => array( 'Order' => array( 'order_id' => '23567636', 'user_id' => '1', 'order_date' => '2013-11-16 16:03:00', 'payment' => 'mc', 'discount_id' => '2', 'total' => '599', 'benefit' => '7212', 'number' => '19' ) )
)
но я хочу:
Как я могу это исправить? заранее спасибо.