using sql union on same table in cakephp find query

不想你离开。 提交于 2019-12-11 09:22:08

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/16140208/using-sql-union-on-same-table-in-cakephp-find-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!