cakephp , the subquery (2)
Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率。
但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些地方讲的不够清楚。
StackOverflow上的这个问题不错
http://stackoverflow.com/questions/5770032/cakephp-and-subquery
The Question:
How can I write SQL subquery using cake syntax. I know how to code simple query, but I can't handle subquery.
This is the original query:
SELECT Assumption.id, Referee.id, Referee.first_name, Referee.second_name
FROM referees AS Referee
INNER JOIN ( SELECT a.id, a.referee_id
FROM assumptions a
WHERE a.season_id =7
) AS Assumption ON Referee.id = Assumption.referee_id
The answer
Since you didn't understand the syntax, this is the actual query:
$records = $this->Referee->find('all', array(
'fields' => array(
'Assumption.id', 'Referee.id', 'Referee.first_name', 'Referee.second_name'
),
'joins' => array(
array(
'table' => 'assumptions',
'alias' => 'Assumption',
'type' => 'INNER',
'foreignKey' => false,
'conditions' => array('Referee.id = Assumption.referee_id', 'Assumption.season_id = 7'),
),
),
)
);
Which produces this query:
SELECT
`Assumption`.`id`,
`Referee`.`id`,
`Referee`.`first_name`,
`Referee`.`second_name`
FROM `referees` AS `Referee`
INNER JOIN assumptions AS `Assumption`
ON (`Referee`.`id` = `Assumption`.`referee_id`
AND `Assumption`.`season_id` = 7)
Which provide the results you are looking for.
Sample output:
Array
(
[0] => Array
(
[Assumption] => Array
(
[id] => 1
[0] => Array
(
[id] => 1
[season_id] => 7
[referee_id] => 1
[name] => SomeAssumpton
) ) [Referee] => Array
(
[id] => 1
[first_name] => Ref
[second_name] => one
) ) )
Another Answer
so after what seemed like years (several hours) and reading cake source... if you want to simply type your subquery into your cake conditions...
it uses php stdClass not an array entry... it will just dump your "value" into the query...
$subquery = new stdClass();
$subquery->type = "expression";
$subquery->value = "Product.id IN (select product_id from product_categories where category_id='$category_id' or belongs_to='$category_id')"; $options['conditions'][] = $subquery; <- dump the class into your conditions array!
do normal query $this->table->find('all', $options)
EXAMPLE: (normal cake with subquery quickfix)
//only from my vendor
$options['conditions']['collection_id'] = $vendor_id; //inner join to CollectionProduct
$options['joins'][0] = array(
"table" => "collection_products",
"alias" => "CollectionProduct",
"type" => "INNER",
"conditions" => array(
"Product.id = CollectionProduct.product_id",
),
); //show only from current category
if ($category_id) {
$subquery = new stdClass();
$subquery->type = "expression";
$subquery->value = "Product.id IN (select product_id from product_categories where category_id='$category_id' or belongs_to='$category_id')";
$options['conditions'][] = $subquery;
} else {
//get 18 random items... no category selected?
$options['limit'] = 18;
} return $this->find('all', $options);
|
cakephp , the subquery (2)的更多相关文章
- cakephp , the subquery
Cakephp 框架帮我们做了很多的工作,的确省了我们很多工作,提高了效率. 但是,碰到一些比较复杂的查询时,还是有些问题,官方的cookbook api 有说明一些详细的用法,但感觉还是不太够,有些 ...
- [数据库]cakephp操作ENUM、tinyint等类型的一点说明
之前无法正常更新ENUM类型的数据,感觉是框架函数实现的bug. 问题很诡异,因为INIT的时候是可以成功写入的,没理由UPDATE的时候不成功. 前后琢磨了一下午,发现了一点蛛丝马迹才终于想通.问题 ...
- MySQL----This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'的意思是,这版本的 MySQL 不支持使 ...
- Oracle索引失效问题:WHERE C1='' OR C2 IN(SubQuery),并发请求时出现大量latch: cache buffers chains等待
问题描述: 项目反馈某功能响应时间很长,高峰期时系统整体响应很慢... 获取相应的AWR,问题确实比较严重,latch: cache buffers chains等待,因为这些会话SQL执行时间太长, ...
- cakephp 打印出SQL语句
最近一直在使用cakephp这个框架学习,最近发现了一些问题,就是怎样将SQL语句打印出来进行调试,方法如下: $db=ConnectionManager::getDataSource('defaul ...
- [慢查优化]慎用MySQL子查询,尤其是看到DEPENDENT SUBQUERY标记时
案例梳理时间:2013-9-25 写在前面的话: 在慢查优化1和2里都反复强调过 explain 的重要性,但有时候肉眼看不出 explain 结果如何指导优化,这时候还需要有一些其他基础知识的佐助, ...
- This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery 解决方法
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'的意思是,这版本的 MySQL 不支持使 ...
- DEPENDENT SUBQUERY” 和 “SUBQUERY”
http://blog.163.com/li_hx/blog/static/183991413201642410122327/ mysql> CREATE TABLE t1 (a INT, b ...
- CakePHP之请求与响应对象
请求与响应对象 请求与响应对象在 CakePHP 2.0 是新增加的.在之前的版本中,这两个对象是由数组表示的,而相关的方法是分散在RequestHandlerComponent,Router,Dis ...
随机推荐
- Android中的Fragment页面切换和selector选择器
效果如图: 提示:下面是用的整个的图片 下面看代码: //--------------------这是主页面布局文件----------------------- <?xml version=& ...
- android性能调优之traceview的使用
1.在开始使用TraceView你要注意: 你的设备和模拟器必须设置SD card 和 你的程序拥有对SD card 具有读写操作的权限( <uses-permission android:na ...
- ios给textView提价提示性文字
不推荐使用的方式 但是在用的时候才发现原来textView没有类似于textField的那种placeholder功能.所谓placeholder就比如用户看到一个输入框,然后输入框里面一般会有几个浅 ...
- 第三十九节,python内置全局变量
vars()查看内置全局变量 以字典方式返回内置全局变量 #!/usr/bin/env python # -*- coding:utf8 -*- print(vars()) #输出 # {'__bui ...
- js算数方法
原文:http://www.w3school.com.cn/js/js_obj_math.asp 除了可被 Math 对象访问的算数值以外,还有几个函数(方法)可以使用. 函数(方法)实例: 下面的例 ...
- c语言编译命令
第14章 预处理及用户配置文件 • 预处理命令通常在程序编译时进行一些符号处 理,其并不执行具体的硬件操作.C51语言中的预 处理命令主要有宏定义指令.文件包指令和条 件编译指令,还有其他一些调试时使 ...
- MySQL半同步复制的数据一致性探讨微信后台团队实践【转】
MySQL是一个RDBMS(关系型数据库管理系统),由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.由于其体积小.速度快.拥有成本低,尤其是开放源码这一特点,广受各大企业欢迎,包括 ...
- User.java 实体类 带 数据库字段模板
package com.tgb.web.controller.entity; import javax.persistence.Column; import javax.persistence.Ent ...
- linux c中select使用技巧
1.select函数作为定时器使用 it_value.tv_sec = 0; it_value.tv_usec = 100000: select(1,NULL,NULL,NULL,& ...
- iOS传值之通知传值(三)
输入所要发送的信息 ,同时将label的值通过button方法调用传递, - (IBAction)buttonClick:(id)sender { //添加 字典,将label的值通过key值设置传递 ...