根据结果​​从多个SQL表中提取值

时间:2015-04-30 15:30:50

标签: php mysql

我需要帮助构建一个Query。我认为解释它的最好方法是完成我需要的工作。

首先,我需要查看r_agent_cstm,根据登录用户的用户名查找ID号。

//Find Agent ID
$result1 = mysql_query("SELECT id_c FROM r_agent_cstm WHERE portalusername_c = '$username'");
    if (!$result1) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    $agent_ID = mysql_fetch_row($result1);
    echo $agent_ID[0]; 
    echo "<br />";

然后我需要找到agent_ID的任何相关帐户ID。

//Find Related Accounts
$result2 = mysql_query("SELECT r_agent_accounts_1accounts_idb FROM r_agent_accounts_1_c WHERE r_agent_accounts_1r_agent_ida = '$agent_ID[0]'");
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    while ($row = mysql_fetch_assoc($result2)) { echo $row['r_agent_accounts_1accounts_idb']; }
    echo "<br />";

不知何故......对于步骤2(上面)中的每个结果,我需要找到更多的ID帐户信息。 (注意:下面我设置了应该是步骤2中的帐户ID的变量,但我不知道如何通过多个结果传递它,所以我设置它只是为了我可以通过逻辑它并测试到目前为止这是有效的。

$testaccount = "1ec39bf0-5ce9-ea23-d362-54c979b75b0f"; //this should be the result(s) from the previous query. 

//Find Related Accounts
$result3 = mysql_query("SELECT companyname_c,status_c FROM accounts_cstm WHERE id_c = '$testaccount'");
    if (!$result3) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    while ($row = mysql_fetch_assoc($result3)) { echo $row['companyname_c']; }
    echo "<br />";

我需要帮助以有效的方式将这些查询拼凑在一起,以每个结果的表格格式回显companyname_cstatus_c(可能只有1个,没有,或者可能有数百个结果)。

0 个答案:

没有答案
相关问题