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?

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.