php mysql 字符串排序

luchongzhi 2009-01-13 04:11:41
我在数据库中取出结果集的形式使这种:

id name remarks price
1 联想电脑1 私塾 4500
1 联想电脑2 私塾 4000
1 联想电脑3 私塾 4600
1 联想电脑4 私塾 4200
1 联想电脑5 私塾 4300
1 联想电脑6 私塾 4400


这样取出来后如何根据price升序排列呢?

我 select * from _goods order by price asc 这样根本就排列不到.我的price是varchar型的.各位帮帮我啊...我把结果集存到array里排序
$data[]=$row['price'];
sort($data,SORT_NUMERIC);
print_r($data);
结果是:
Array ( [0] => 15000 )
Array ( [0] => 5700 [1] => 15000 )
Array ( [0] => 4500 [1] => 5700 [2] => 15000 )
Array ( [0] => 4500 [1] => 5100 [2] => 5700 [3] => 15000 )
Array ( [0] => 4000 [1] => 4500 [2] => 5100 [3] => 5700 [4] => 15000 )

各位高手指点哈如何根据price来升序排列.先谢过了.....我太菜了.....
...全文
733 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Marx-link 2011-11-21
  • 打赏
  • 举报
回复
order by cast(列名 as decimal) asc

这些都不要单引号!
gh1990906 2011-06-20
  • 打赏
  • 举报
回复
我也遇到了这个问题,感谢16楼,谢谢你的答案
frederic_zhao 2009-01-15
  • 打赏
  • 举报
回复

select * from _goods order by price+0 asc


试试
helloyou0 2009-01-15
  • 打赏
  • 举报
回复
典型的自找苦吃....


正确答案见13楼





dandande 2009-01-14
  • 打赏
  • 举报
回复
你是想把数据库中取出的结果集按下面的顺序输出吗?

id name remarks price
1 联想电脑2 私塾 4000
1 联想电脑4 私塾 4200
1 联想电脑5 私塾 4300
1 联想电脑6 私塾 4400
1 联想电脑1 私塾 4500
1 联想电脑3 私塾 4600
dandande 2009-01-14
  • 打赏
  • 举报
回复
$sql="select * from _goods";
$result=mysql_query($sql);

while($row=mysql_fetch_row($result)){ //把每行的结果放入数组,这里共有4个1维数组
$i++; //出循环后,$i=记录集的总行数
$id[$i]=$row[0];
$name[$i]=$row[1];
$remarks[$i]=$row[2];
$price[$i]=$row[3];
}

for($j=1;$j<$i;$j++){ //按$price从小到大输出
for($k=$j+1;$k<$i+1;$k++){
if($price{[$j]>$price[$k]){
$a=$id{[$j]; $id{[$j]=$id[$k];$id[$k]=$a;
$a=$name{[$j]; $name{[$j]=$name[$k];$name[$k]=$a;
$a=$remarks{[$j]; $remarks{[$j]=$remarks[$k];$remarks[$k]=$a;
$a=$price{[$j]; $price{[$j]=$price[$k];$price[$k]=$a;
}
}
echo $id[$j].'.'.$name[$j].'.'.$remarks[$j].'.'.$price[$j].'<br>';
}
luchongzhi 2009-01-14
  • 打赏
  • 举报
回复
大清早的又来麻烦各位了.....帮帮忙啊.....我排不出来.....
foolbirdflyfirst 2009-01-14
  • 打赏
  • 举报
回复
1.取出来,解密
2.array_multisort(手册一下)

luchongzhi 2009-01-14
  • 打赏
  • 举报
回复
10樓的正是我想要的結果....但是我的價格是varchar型的...order by 根本不起作用....所以來請教各位高手,先謝過了.....
cgisir 2009-01-14
  • 打赏
  • 举报
回复
查询里最后加个 order by price desc
luchongzhi 2009-01-13
  • 打赏
  • 举报
回复
高手们,来帮帮忙啊......这个世界那么多热心的人.....谢谢了
luchongzhi 2009-01-13
  • 打赏
  • 举报
回复
因为价格这个数据太敏感了...我把他加密了......不是md5...自己加密的....
jim8590251 2009-01-13
  • 打赏
  • 举报
回复
LZ想用数字的大小排序,那为什么不把该字段设置成数字类型的呢?
free999 2009-01-13
  • 打赏
  • 举报
回复
冒泡
$old = Array ( 4000 ,4500 , 5100 , 5700 , 15000, 3,500, 2000 ) ;
for($i=0;$i<count($old);$i++)
for($j=0;$j<count($old)-1-$i;$j++)
if($old[$j]<$old[($j+1)])
list($old[$j],$old[($j+1)]) = array($old[($j+1)], $old[$j]);

print_r($old);//大到小

$old = Array ( 4000 ,4500 , 5100 , 5700 , 15000, 3,500, 2000 ) ;
for($i=0;$i<count($old);$i++)
for($j=0;$j<count($old)-1-$i;$j++)
if($old[$j]>$old[($j+1)])
list($old[$j],$old[($j+1)]) = array($old[($j+1)], $old[$j]);

print_r($old);//小到大
luchongzhi 2009-01-13
  • 打赏
  • 举报
回复

$sql="select * from _itemtypeinfo,_suppliersinfo,_purchasegoodsinfo,_goodspriceinfo where
_suppliersinfo.tid=_itemtypeinfo.id and _purchasegoodsinfo.sid=_suppliersinfo.id and
_purchasegoodsinfo.tid=_itemtypeinfo.id and _goodspriceinfo.gid=_purchasegoodsinfo.id
and _itemtypeinfo.state!=1 and _goodspriceinfo.state!=1 and _purchasegoodsinfo.state!=1 and _suppliersinfo.state!=1 and _itemtypeinfo.typename like '%".$content."%' or
_suppliersinfo.supplyname like '%".$content."%' or _purchasegoodsinfo.goodsname like '%".$content."%' ";
}
$result=mysql_query($sql) or die("SQL Error: <br>".$sql."<br>".mysql_error());
$count=mysql_num_rows($result);
if($count!=0){

/*function ORD_ASC($a,$b){
if($a==$b){
return 0;
}
return ($a<$b)?-1:1;
}
*/
while($row=mysql_fetch_array($result)){
$data[]=$row['price'];
//$data1[]=$row['id'];
//$array=array("id"=>$data1,"price"=>$data);
//usort($data,"ORD_ASC");
sort($data,SORT_NUMERIC);
print_r($data);
//echo sizeof($array['price']);
//echo $data[0];

//print_r($data);
/* foreach($data as $price)*/
/*$count=count($data);*/
for($i=0;$i<count($data[0]);$i++){}
?>
<tr>
<td><input type="checkbox" name="select[]" value="" /></td>
<td><?=$row['typename']?></td>
<td><?=$row['goodsname']?></td>
<td><?=$row['supplyname']?></td>
<td><? echo $data[$i]; ?></td>
<td><?=$row['tel1']."/".$row['tel2']."/".$row['tel3']."/".$row['tel4'] ?></td>
</tr>
<?
}
/* }
}
}
}*/
}
else{
?>
<tr>
<td colspan="6"><font color="blue">很抱歉,沒有相關的資料.</font></td>
</tr>
<?
}
?>


楼上兄台给我的那个运行结果是:
Array ( [0] => 15000 )
Array ( [0] => 5700 [1] => 15000 )
Array ( [0] => 4500 [1] => 5700 [2] => 15000 )
Array ( [0] => 4500 [1] => 5100 [2] => 5700 [3] => 15000 )
Array ( [0] => 4000 [1] => 4500 [2] => 5100 [3] => 5700 [4] => 15000 )

我向的是在把价格遍历出来写到表格里去,而且是按照价格升序排列的....谢谢了......
ichigoxi 2009-01-13
  • 打赏
  • 举报
回复

$data[]=$row['price'];
sort($data,SORT_NUMERIC);
print_r($data);
print_r(array_reverse($data));
luchongzhi 2009-01-13
  • 打赏
  • 举报
回复
不是很明白呢?能给我个例子学习下么?
CuTe_CuBe 2009-01-13
  • 打赏
  • 举报
回复
把你现在排好的东西用array_reverse反转一下。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧