Dani Dani - 7 months ago 12
SQL Question

I want to pull data from an array in specific sequence using FIND_IN how?--never recived a right answer

Below is my code and I want to pull the data based on the sequence 3, 10 then 7, how can I do that? so far it pulls first 10, then 7, then 3.

$cars = $this->car->find('all', array(
'conditions' => array(
'car.id' => array(3, 10, 7)
),
'limit' => 3,
'order' => array('car.id' => 'desc')
));

Answer

Where do the conditions come from? Anyway i believe this will work:

    $cars = $this->car->find('all', array(
    'conditions' => array(
        'car.id' => array(3, 10, 7)
    ),
    'limit' => 3, 
    'order' => array('FIELD(car.id,3,10,7)')
));

Might need to play around with the cakePHP specific syntax, but the ORDER BY FIELD(fieldname, value, value value); should work

Comments