简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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