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

标签 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', '回调'、'对讲机'、'queuecentre')

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

我怎样才能解决这个问题,让它按我想要的方式排序?

最佳答案

在我看来,您在错误的位置对查询进行了排序,但不知道您的预期输出是什么,这是对 $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 并正确转义您的数据。

关于PHP/MySQL SELECT 按字段(value1,value2)等查询和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18705738/

相关文章:

php - 如何使用脚本硬编码附加到 PHP 文件上的数组?

MySQL SQL 查询分析和查询成本工具和技术

php - 将评论限制为 250 个字符

php - 在 Drupal 中使用 Apache Solr 按版本索引而不是节点

phpinfo() 输出中的 PHP 版本错误

php - jQuery, PHP : Calling a PHP script from jQuery, 但它似乎没有执行

php - 在 PHP 中每行添加 4 个项目

mysql - 导入mysql数据库出错

mysql - CodeIgniter- 事件记录插入新的或更新重复

python - 是否可以使用 Google Cloud Datastore 批量插入