在cakephp的同一表上使用sql联合查找查询 [英] using sql union on same table in cakephp find query

查看:54
本文介绍了在cakephp的同一表上使用sql联合查找查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的查询:

 (SELECT * FROM user WHERE id< 5 order by id DESC限制1)
UNION
(选择*来自用户WHERE ID = 5)
UNION
(选择*来自用户WHERE ID> 5限制1)

如何将上述查询转换为 CakePHP find('all')查询?



谢谢

解决方案

使用Model :: find('neighbors')


将其重写为find('all')是有问题的,但是 find('neighbors')可能符合您的要求;


请参阅文档 find('neighbors')


这应该为您提供所需的数据,但是您可能需要稍微采用一些代码才能拥有它工作ng正确地使用返回数组的不同布局;

  $ data = $ this-> User-> find('neighbors' ,array('field'=> ‘id’,‘value’=> 5)); 

更新


刚刚看到您在CakePHP 1.3,上面发布的链接指向文档的2.x部分。虽然类似,但这是CakePHP 1.3的文档:


http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html#find-neighbors


let's say I have a query like this:

(SELECT * FROM user WHERE id < 5 order by id DESC LIMIT 1)
UNION
(SELECT * FROM user WHERE id = 5)
UNION
(SELECT * FROM user WHERE id > 5  LIMIT 1)

How can I translate the above query into a CakePHP find('all') query?

Thank you

解决方案

Using Model::find('neighbors')

Rewriting this to a find('all') will be problematic, however find('neighbors') may fit your requirements;

See the documentation find('neighbors')

This should give you the data you need, but you will probably have to adopt your code a bit to have it working correctly with the different 'layout' of the returned array;

 $data = $this->User->find('neighbors', array('field' => 'id', 'value' => 5));

update

Just saw you're on CakePHP 1.3, the link posted above points to the 2.x part of the documentation. Although similar, this is the documentation for CakePHP 1.3:

http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html#find-neighbors

这篇关于在cakephp的同一表上使用sql联合查找查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆