“按字段顺序”MySQL似乎不起作用

时间:2012-05-01 18:25:20

标签: php mysql sql-order-by

长话短说,我的字段顺序似乎不适用于我的MySQL调用。我无法在存储中找到一个模式...

$ theorder正确设置为

, 'People-Wedding Photo', 'People-Kids', 'People-Male/Female', 'Architecture-Exterior', 'Architecture-Interior', 'Animal-Birds', 'Backgrounds'

这是我的代码。正如你所看到的,我正在循环,做我的格式转换....但我仍然得到一个错误的订单...一个随机的订单....

我有什么遗失的吗?

$under_link_query = mysql_query("SELECT DISTINCT(type), type FROM ".$prefix."ProFolio_work WHERE type != 'Backgrounds' ORDER BY FIELD(type" . $theorder .")");


    while($u_row = mysql_fetch_assoc($under_link_query)){
        $type = html_entity_decode($u_row['type']);

        $typeForm = preg_replace('/[^a-z0-9]/i', '_', $type);
        ?>
           <a href="#" id="link_<? echo str_replace(' ', '_', $typeForm); ?>">
    <? echo ucfirst($type); ?></a>
            <?

1 个答案:

答案 0 :(得分:2)

@Malovich正在回答,基本上你应该使用:

ORDER BY $theorder

但请记住,默认排序是升序,顺序是按顺序排序。我已经注意到你的变量$theorder被设置为以逗号开头的字符串,这几乎是你的问题。

确保$theorder不以逗号开头,然后尝试在一行上排序。如果可行,请继续添加更多。

===============编辑=================

道歉,你正试图对一个字段进行子排序。错过了这一部分,所以这就是正在发生的事情:

您需要提供所有要排序的字段,否则MySQL会对您先未提供的每个字段进行排序,然后它会对您提供的字段进行排序。如果需要,您可以提供要订购的字段子集,但是您需要以相反的顺序提供它们,然后使用DESC表示法,如:

ORDER BY FIELD (field,'lastsort',middlesort','firstsort') desc

按以下顺序显示您的列表:

firstsort items
middlesort items
lastsort items
<all other options, in reverse alphabetical order> items

这个关键部分是你需要添加所有字段值才能进行排序。 (或使用解决方法)。

相关问题