dourao1896 2013-09-09 19:43
浏览 99
已采纳

PHP / MySQL SELECT查询和按字段排序(value1,value2)等

I have some SQL Queries, the first one selects the customer, second one selects the list of extensions.

<?php
$sql2="SELECT * from client where parent_client_id = '1234' "; $rs2=mysql_query($sql2,$pbx01_conn) or die(mysql_error()); $count = mysql_num_rows($rs2); ?>

<table width="600" border="0" cellspacing="5" cellpadding="5">
    <tr>
        <td colspan="5"><strong>My VoIP Extensions (<?php echo $count; ?>)</strong></td>
    </tr>
    <tr>
        <td width="30%"><strong>Name</strong></td>
        <td width="30%"><strong>Extension Number</strong></td>
        <td width="30%"><strong>Type</strong></td>
    </tr>
    <?php
    while($result2=mysql_fetch_array($rs2))
    {
        $sql3="SELECT * from extension where client_id = '".substr($result2["id"],1)."' ORDER BY FIELD(type, 'term', 'queue', 'ivr', 'voicecentre', 'conference', 'callback', 'intercom', 'queuecentre') ";
        $rs3=mysql_query($sql3,$pbx01_conn) or die(mysql_error());
        $result3=mysql_fetch_array($rs3);

        if($result3["type"] == 'term')
        {
            $type = 'Phone Terminal';
        }
        elseif($result3["type"] == 'queue')
        {
            $type = 'Queue';
        }
        elseif($result3["type"] == 'ivr')
        {
            $type = 'IVR';
        }
        elseif($result3["type"] == 'voicecenter')
        {
            $type = 'Voicemail Centre';
        }
        elseif($result3["type"] == 'conference')
        {
            $type = 'Conference';
        }
        elseif($result3["type"] == 'callback')
        {
            $type = 'Callback';
        }
        elseif($result3["type"] == 'intercom')
        {
            $type = 'Intercom';
        }
        elseif($result3["type"] == 'queuecenter')
        {
            $type = 'Queue Login Centre';
        }

        echo '<tr class="notfirst" style="cursor:pointer;" onclick="document.location=\'sip:'.$result["customerid"].'*'.$result3["number"].'\'">
                    <td>'.$result2["name"].'</td>
                    <td>'.$result["customerid"].'*'.$result3["number"].'</td>
                    <td>'.$type.'</td>
                  </tr>';
    }
    ?>
</table>

on my $sql3 i have an order by field: ORDER BY FIELD(type, 'term', 'queue', 'ivr', 'voicecentre', 'conference', 'callback', 'intercom', 'queuecentre')

but its not ordering correctly. i think its because the order by clause is not on the while loop.

how can i get round this so it orders how i want it to?

  • 写回答

1条回答 默认 最新

  • dougu0824 2013-09-09 19:50
    关注

    It looks to me like you are ordering your query in the wrong place, but without knowing what your expected output is, this is a guess at what you $sql2 query should be:

    SELECT client.* from client where parent_client_id = '1234'
    JOIN extension on extension.client_id = client.client_id
    ORDER BY FIELD(extension.type, 'term', 'queue', 'ivr', 'voicecentre', 'conference', 'callback', 'intercom', 'queuecentre')
    

    Also - PLEASE note the comment by @tadman and use mysqli and properly escape your data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退