3

我正在运行这个 SQL 代码:

sql=" SELECT * FROM channel_did ";
        $rs=mysql_query($sql,$pbx01_conn) or die(mysql_error());
        $counter=0;
        $display='';
        while($result=mysql_fetch_array($rs))
        {
            $sql2="SELECT * from client where id = '".$result["client_id"]."' ";
            $rs2=mysql_query($sql2,$pbx01_conn) or die(mysql_error());
            $result2=mysql_fetch_array($rs2);
}

所以在channel_did表中client_id列是一个数字然后我在client表中查找 id 等于channel_id.client_id

我怎样才能得到列表(从表中的列channel_did)排序?companyclient

**channel_id**
id
did
client_id

**client**
id
name
company

so client.id = channel_did.client_id
4

2 回答 2

3

使用两个表编写查询,inner join从第一个表中选择字段,然后使用第二个表的列对行进行排序

SELECT a.* FROM channel_did a
INNER JOIN client b on a.client_id = b.id
ORDER BY b.company 

当然,在这种情况下client,每行对应的只有一行channel_did

希望能帮助到你。

于 2013-07-25T15:42:39.173 回答
1

尝试这样的事情..

 SELECT i.* FROM `channel_did` AS i
    JOIN `client` AS c
    ON (i.client_id = c.id)
ORDER BY c.company DESC
于 2013-07-25T15:46:56.840 回答