大家好,我想从数据库中取出一系列产品的资料,因为产品ID不确定,我把要求的id放在一个数组中,例如
array ( 1678, 1679, 1680, 1681, 1682, 1683, 489, 490, 491, 2314, 2513, 1223, 2739, 2315, 2316, 2307, 2318, 2112, 2308, 2309 )当我用这个条件到Mysql中查数据里, 发现返回的结果是按产品ID的大小排序了,
489,490,491...我不想这样,想仍然按我刚才的顺序返回结果,
p.products_id
IN ( 1678, 1679, 1680, 1681, 1682, 1683, 489, 490, 491, 2314, 2513, 1223, 2739, 2315, 2316, 2307, 2318, 2112, 2308, 2309 )可是Mysql会自动排序,怎么解决?有什么语句可以实现吗?多谢.

解决方案 »

  1.   

    select * from `products` p where p.products_id
    IN ( 1678, 1679, 1680, 1681, 1682, 1683, 489, 490, 491, 2314, 2513, 1223, 2739, 2315, 2316, 2307, 2318, 2112, 2308, 2309 ) order by find_in_set(p.products_id,'1678, 1679, 1680, 1681, 1682, 1683, 489, 490, 491, 2314, 2513, 1223, 2739, 2315, 2316, 2307, 2318, 2112, 2308, 2309') asc;
      

  2.   

    ...,撑开了。。,再发一次。
    =======================================================
    select * from `products` p where p.products_id
    IN ( 1678, 1679, 1680, 1681, 1682, 1683, 489, 490, 491, 2314, 2513, 1223, 2739, 2315, 2316, 2307, 2318, 2112, 2308, 2309 ) 
    order by find_in_set(p.products_id,'1678, 1679, 1680, 1681, 1682, 1683, 489, 490, 491, 2314, 2513, 1223, 2739, 2315, 2316, 2307, 2318, 2112, 2308, 2309') asc;
      

  3.   

    多谢,可以了,原来要把空格去掉.SELECT products_id
    FROM `products` p
    WHERE p.products_id
    IN ( 1678,1679,1680,1681,1682,1683,489,490,491,2314,2513,1223,2739,2315,2316,2307,2318,2112,2308, 2309 )
    ORDER BY find_in_set( p.products_id, '1678,1679,1680,1681,1682,1683,489,490,491,2314,2513,1223,2739,2315,2316,2307,2318,2112,2308,2309' )