问题
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