dtttlua7165 2013-06-27 13:27
浏览 140
已采纳

SQL首先获取一段时间内选择的id,然后从表中获取其他ID?

I need to put first selected item in a list, and then the rest in order by id.

My table:

id:1 car: fiat
id:2 car:ford
id:3 car:vw
id:4 car:toyota

the problem in .com/index.php?car=fiat

$car = $_GET['car'];
$results = mysqli_query($connecDB,"select * from products order by (case id when 999 then 0 else 1 end), car asc");

and the body

while($row = mysqli_fetch_array($results )){
echo '
    <div id="'.$row["car"].'"></div>
';
}

In example:
if I get the value 3:
ID
3
1
2
4
5

if I get the value 4:
ID
4
1
2
3
5

I dont know where to put the variable $car to work properly.
Thanks and i apoligize for my english.

  • 写回答

1条回答 默认 最新

  • drvc63490 2013-06-27 14:15
    关注

    If the ID is numeric and will always be >0, this will work (I've kept the parentheses from your original query; note they're optional):

    select *
    from products
    order by (case id when 3 then 0 else id end), car asc
    

    If the ID can have negative values then something like this would be safer:

    select *
    from products
    order by (case id when 3 then 0 else 1 end), id, car asc
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!