PHP/MySQL在其他表中按列排序 [英] PHP/MySQL Order by column in a different table

查看:90
本文介绍了PHP/MySQL在其他表中按列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下SQL代码:

I am running this SQL Code:

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

so in the channel_did table the client_id column is a number then i do a lookup in the client table where the id equals the channel_id.client_id

如何获取列表(从channel_did)client表中的company列进行排序?

how can i get the list (from channel_did) to order by the company column in the client table?

**channel_id**
id
did
client_id

**client**
id
name
company

so client.id = channel_did.client_id

推荐答案

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

write a query with an inner join with both tables, select the fields from the first table, and use a column of the second table to sort your rows

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

当然是在client中每一行channel_did

希望有帮助.

这篇关于PHP/MySQL在其他表中按列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆