Home  >  Article  >  Backend Development  >  又一个难题,数据有些联系,但不好做解决思路

又一个难题,数据有些联系,但不好做解决思路

WBOY
WBOYOriginal
2016-06-13 13:42:55884browse

又一个难题,,数据有些联系,但不好做
还是查询

一张表有n个字段 ,其中主键id


然后php代码中有一个数组

$arr=array(
  array('id'=>'5','num'=>100),
  array('id'=>'11','num'=>99),
  array('id'=>'3','num'=>98),
......
)

也就是说$arr 这个数组根据num由大到小排列,,此时的id顺序是这样的,

然后要求是数据库查询集合要根据这个id排列的顺序,查找对应的集合,怎么做,,

看上去有点无规则,但好像又有点联系

------解决方案--------------------
是把id在数组里id的查出来吗??

如果是:把id组成一个array,然后sql id in (implode())
------解决方案--------------------
将数组导入临时表
与源表做关联查询,联接键 id,排序键 临时表.num
------解决方案--------------------
如果有数组 $arr=(3,2,1); 和 表 table,如下sql可以实现表table 按 $arr排序

SQL code
SELECT t.* FROM `table` t,(SELECT 3 AS id UNION SELECT 2 UNION SELECT 1) AS arr WHERE t.id=arr.id
<br><font color="#e78608">------解决方案--------------------</font><br>order by 可以实现根据你提供的数值来排序的,不过豸像效率不乍样
<br><font color="#e78608">------解决方案--------------------</font><br>这个最好是交给php来排。 <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn