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

时间:2013-09-09 19:43:26

标签: php mysql

我有一些SQL查询,第一个选择客户,第二个选择扩展名列表。

<?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>

在我的$sql3我有一个按字段排序:ORDER BY FIELD(type, 'term', 'queue', 'ivr', 'voicecentre', 'conference', 'callback', 'intercom', 'queuecentre')

但它没有正确排序。我认为是因为order by子句不在while循环中。

我如何绕过这个,以便命令我想要它?

1 个答案:

答案 0 :(得分:0)

在我看来,您在错误的地方订购查询,但不知道您的预期输出是什么,这是对您的$ sql2查询应该是什么的猜测:

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')

另外 - 请注意@tadman的评论并使用mysqli并正确转义您的数据。

相关问题