【发布时间】:2015-10-16 10:28:44
【问题描述】:
我遇到了一个非常不寻常的排序问题。我必须按照我现在在数据库中的预定义位置来排列项目:
sid - auto increment
name - just something to distinguish varchar 100
sorting - this is where we define the exact position of the item
如果我们给任何产品sorting = 2,无论如何它都会排在第二位。但它应该是由排序定义的。
我无法使用order by sorting desc 排列项目,因为大多数项目都没有分配任何位置。由于某些赋值的条目有sorting = 0,因此按照计划,所有排序值不等于0的条目都应该放在其他条目之前。
这是当前代码:
<?php
$conn=mysqli_connect("localhost","root","","work");
$getthedetails=mysqli_query($conn,"select sid,sorting,name from sorting order by sorting asc");
while($thelistis=mysqli_fetch_array($getthedetails)){ ?>
<div style="width:100px;font-weight:bold;color:#000;font-size:19px;font- family:arial;height:100px;padding:49px;background:silver;float:left;margin:10px;">
<?=$thelistis['sorting']?>
</div>
<?php } ?>
理想情况下,我希望排序为 1,0,0,4,5,0,0,8。
我想要的是this。
【问题讨论】:
-
试试
ORDER BY -sorting DESC -
@TerisL 它将无法正常工作,因为我一直在使用它。它不起作用,因为排序对于未排序的项目可以有 value=0
-
列和表都命名为“排序”?
-
是的......但这不是任何问题,我将更改表名,这只是为了测试目的
-
不知道是否可以通过查询,我会处理一些事情并用PHP告诉你
标签: php mysql sorting sql-order-by