php - 如何连接到具有特定条件的表

标签 php mysql

我有两个表:帐户和授权。

帐户表包含客户和代理的全名和 ID。授权表包含客户和该客户下的代理的 ID。帐户表包含代理和客户的全名和 ID。

我不断获取代理名称而不是客户名称。不知道如何解决这个问题!

更新:我能够获得正确的 ID,但客户的姓名仍然没有显示。
代码:

//Query
$sql = "SELECT authorizations.customer_id, accounts.fullname
        FROM authorizations
        INNER JOIN accounts ON
        authorizations.system_id = accounts.authorizating_customer
        WHERE authorizations.agent_id = '$did'";

$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "";
$i=0;
while ($i < $num) {
    $name = mysql_result($result, $i, "fullname");
    echo "";
    echo "";
    $i++;
}

最佳答案

尝试

$sql = "SELECT 
            authorizations.customer_id, 
            accounts.fullname 
        FROM 
            authorizations 
                LEFT JOIN 
                    accounts 
                ON 
                    accounts.authorizing_person = authorizations.customer_id 
        WHERE 
            authorizations.agent_id = '$agentid' 
        ORDER BY 
            accounts.id DESC";

$result = mysql_query($query);

$num = mysql_numrows($result);

mysql_close();

echo "";

$i=0;
while ($i < $num) {

    $name = mysql_result($result,$i,"fullname");
    echo "";
    echo "";
    $i++;
}

关于php - 如何连接到具有特定条件的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6098620/

相关文章:

php - 如何从字符串中提取非分隔日期和时间

mysql - 备份 MySQL 数据库的安全方法?

mysql - 我的 spring+hibernate 应用程序不关闭 jdbc 连接

php - Codeigniter 不允许我更新条目,因为某些字段必须是唯一的

php - 使用特定类别或门票参加运行比赛

php比较两个sql表并寻找差异

php - 检查图像修改时间的 HTACCESS 图像缓存规则

php - 电子邮件存在功能与 codeigniter php

mysql - 通过 phpmyadmin 从 MYISAM 切换到 InnoDB 非常慢

php - 使用 session ID 访问 session 数据