我需要基于数组中的精确顺序来拉取数据 [英] I need to pull data based on its exact sequence in an array
问题描述
下面是我的代码,我想根据序列3,10然后7,拉我的数据,我怎么能这样做?到目前为止它先前10,然后7,然后3。
< code>
$ cars = $ this-> car-> find('all',array(
'conditions'=> array(
'car.id'=> array 3,10,7)
),
'limit'=> 3,
'order'=> array('car.id'=>'desc')
));
< / code>
在数组中。在这种情况下,MySQL FIND_IN_SET
函数可以帮助你。
您可以添加以下order by语句:
ORDER BY FIND_IN_SET(car.id,'3,10,7')
pre>
注意:您需要在您的等效
蛋糕中转换这个order by语句php mysql
查询。
MySQL FIND_IN_SET()返回字符串的位置(如果存在)
(作为子字符串) 。字符串列表本身是一个
字符串包含用','(逗号)字符分隔的子字符串。
当搜索字符串不存在于
字符串列表,如果任一参数为NULL,则返回NULL。
:
SELECT *
FROM cars
id
2
3
4
5
6
7
8
9
10
11
输出 / p>
查询:
SELECT *
FROM cars
WHERE cars.id IN(3,10,7)
ORDER BY FIND_IN_SET(cars.id,'3,10,7')
id
3
10
7
我不知道
CAKE PHP语法
在构建mysql查询。
但是
cake php mysql
中的等价查询可能是这样的:$ cars = $ this-> car-> find('all',array(
'conditions'=> array(
'car.id'=> array(3,10,7)
),
'limit'=& 3,
'order'=> array(FIND_IN_SET('car.id','3,10,7'))
));
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.
<code> $cars = $this->car->find('all', array( 'conditions' => array( 'car.id' => array(3, 10, 7) ), 'limit' => 3, 'order' => array('car.id' => 'desc') )); </code>
解决方案The idea is to order the result by their respective position in the array. In this case MySQL
FIND_IN_SET
function can help you.You may add the following order by statement:
ORDER BY FIND_IN_SET(car.id,'3,10,7')
Note: You need convert this order by statement in your equivalent
cake php mysql
query.MySQL FIND_IN_SET() returns the position of a string if it is present (as a substring) within a list of strings. The string list itself is a string contains substrings separated by ‘,’ (comma) character.
This function returns 0 when search string does not exist in the string list and returns NULL if either of the arguments is NULL.
Sample Input:
query:
SELECT * FROM cars
id 2 3 4 5 6 7 8 9 10 11
Output:
query:
SELECT * FROM cars WHERE cars.id IN (3,10,7) ORDER BY FIND_IN_SET(cars.id,'3,10,7')
id 3 10 7
Check the SQLFIDDLE DEMO here
Edit:
I don't know
CAKE PHP syntax
in building mysql query.But The equivalent query in
cake php mysql
may be something like that:$cars = $this->car->find('all', array( 'conditions' => array( 'car.id' => array(3, 10, 7) ), 'limit' => 3, 'order' => array(FIND_IN_SET('car.id' , '3,10,7')) ));
这篇关于我需要基于数组中的精确顺序来拉取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!