0

我创建了一个用于显示报告的查询。该查询从几个不同的表中获取数据,这很好,尽管我无法弄清楚如何在一个表中回显多个匹配值。例如,表 vtiger_addisa 有多个我需要显示的 newcurrentamount。

$sql = $adb->query("SELECT *
                FROM vtiger_isa, vtiger_addisa, vtiger_contactdetails
                WHERE vtiger_isa.relatedclient = vtiger_addisa.addrelatedclient
                AND vtiger_addisa.addrelatedclient = vtiger_contactdetails.contactid
                AND vtiger_isa.relatedclient = $relatedclient
                AND vtiger_isa.policynumber = $policynumber");


            //Uncomment lines below for testing sql             
             //echo $sql;
            //exit; 
            while ($sql->fetchInto($row)) {
    // Assuming DB's default fetchmode is DB_FETCHMODE_ORDERED
    echo $row['firstname'] . "\n"; 
    echo $row['lastname'] . "\n"; 
    echo $row['policynumber'] . "\n"; 
    echo $row['newcurrentamount'] . "\n"; 
    echo $row['newcurrentamount'] . "\n"; 
    echo $row['currentamount'] . "\n"; 



   exit;

} 
4

2 回答 2

1

您可以获得多行结果,如下所示。

global $adb;
$sql = $adb->query("SELECT * FROM vtiger_isa, vtiger_addisa, vtiger_contactdetails
    WHERE vtiger_isa.relatedclient = vtiger_addisa.addrelatedclient
    AND vtiger_addisa.addrelatedclient = vtiger_contactdetails.contactid
    AND vtiger_isa.relatedclient = $relatedclient
    AND vtiger_isa.policynumber = ?");

    $params = array($policynumber);
    $result = $adb->pquery($query, $params);
    $noOfRows = $db->num_rows($result);

    for($i=0; $i<$noOfRows; ++$i) {
        $firstname =  $db->query_result($result, $i, "firstname");
        $lastname =  $db->query_result($result, $i, "lastname");
        $policynumber =  $db->query_result($result, $i, "policynumber");
        $newcurrentamount =  $db->query_result($result, $i, "newcurrentamount");
        $currentamount =  $db->query_result($result, $i, "currentamount");
    }
于 2017-04-29T13:36:38.253 回答
0

使用“as”运算符

select newcurrentamount as a, newcurrentamount as b from vtiger_addisa

PS-尽量避免使用“*”并仅使用必要的列。

于 2017-04-17T18:12:59.017 回答