简体   繁体   English

在cakephp的同一表上使用sql联合查找查询

[英]using sql union on same table in cakephp find query

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? 如何将上述查询转换为CakePHP find('all')查询?

Thank you 谢谢

Using Model::find('neighbors') 使用Model :: find('neighbors')

Rewriting this to a find('all') will be problematic, however find('neighbors') may fit your requirements; 将其重写为find('all')将是有问题的,但是find('neighbors')可能符合您的要求;

See the documentation find('neighbors') 参见文档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. 刚刚看到您使用的是CakePHP 1.3,上面发布的链接指向文档的2.x部分。 Although similar, this is the documentation for CakePHP 1.3: 尽管类似,但这是CakePHP 1.3的文档:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM