加入两个mysql_fetch_arrays

时间:2012-12-05 23:01:39

标签: php mysql sql

我正在尝试将两个查询拼凑在一起。下面是我正在使用的代码。但是该表正在拆分数据。我该如何解决这个问题?或者有更好的解决方案?

while($row = mysql_fetch_array($result))
{
    echo "<tr id='centered' >";  
    echo "<td class='leftalign'>" . $row['Quarter_Name'] . "</td>";
    echo "<td>" . $row['Quarterly_yield'] . "</td>";
    echo "<td>" . $row['Quarterly_yield'] . "</td>";
    echo "<td>" . $row['Quarterly_yield'] . "</td>";
}

while($row = mysql_fetch_array($result8))
{
    echo "<td>" . $row['Quarterly_yield'] . "</td>";
} 

2个查询如下:它们几乎相同

SELECT  LEFT(A.F_ANOTRIMESTRE, 4) Year,
        RIGHT(A.F_ANOTRIMESTRE, 2) Quarter,
        IF(RIGHT(A.F_ANOTRIMESTRE, 2)=03,'Enero a Marzo',
            IF(RIGHT(A.F_ANOTRIMESTRE, 2)=06,'Abril a Junio',
                IF(RIGHT(A.F_ANOTRIMESTRE, 2)=09,'Julio a Septiembre',
                    IF(RIGHT(A.F_ANOTRIMESTRE, 2)=12,'Octubre a Diciembre', '')
                )
            )
        ) Quarter_Name,
        ROUND(A.POR_RENTABILIDAD, 2) Quarterly_yield
FROM    dr_rent_carteras_trimestres A
WHERE   A.ID_CARTERA = $ID_CARTERA
AND     A.IND_RENTABILIDAD = 1
AND     LEFT(A.F_ANOTRIMESTRE, 4) = (
            SELECT MAX(left(F_ANOTRIMESTRE, 4)) - 0
            FROM   dr_rent_carteras_trimestres
            WHERE  ID_CARTERA = $ID_CARTERA 
        )

这是第二个:

SELECT  LEFT(A.F_ANOTRIMESTRE, 4) Year,
        RIGHT(A.F_ANOTRIMESTRE, 2) Quarter,
        IF(RIGHT(A.F_ANOTRIMESTRE, 2)=03,'Enero a Marzo',
            IF(RIGHT(A.F_ANOTRIMESTRE, 2)=06,'Abril a Junio',
                IF(RIGHT(A.F_ANOTRIMESTRE, 2)=09,'Julio a Septiembre',
                    IF(RIGHT(A.F_ANOTRIMESTRE, 2)=12,'Octubre a Diciembre', '')
                )
            )
        ) Quarter_Name,
        ROUND(A.POR_RENTABILIDAD, 2) Quarterly_yield
FROM    dr_rent_carteras_trimestres A
WHERE   A.ID_CARTERA = $ID_CARTERA
AND     A.IND_RENTABILIDAD = 1
AND     LEFT(A.F_ANOTRIMESTRE, 4) = ( 
            SELECT MAX(left(F_ANOTRIMESTRE, 4)) - 1
            FROM   dr_rent_carteras_trimestres
            WHERE  ID_CARTERA = $ID_CARTERA 
        )

1 个答案:

答案 0 :(得分:1)

在上面的代码中,您在第一个循环中打开tr标记,但从不关闭它。同样在第一个循环中,您打印出Quarterly_yield三次,但是为该结果计算了一列。实际上你打印出同样的东西。

几个选项 - 在每行中打印每个查询的结果:

// Loop through the results and print 
while($row = mysql_fetch_array($result))
{
    echo "<tr id='centered' >\m";  
    echo "<td class='leftalign'>{$row['Quarter_Name']}</td>\n";
    echo "<td>{$row['Quarterly_yield']}</td>\n";
    echo "</tr>\n";
}

while($row = mysql_fetch_array($result8))
{
    echo "<tr id='centered' >\m";  
    echo "<td class='leftalign'>{$row['Quarter_Name']}</td>\n";
    echo "<td>{$row['Quarterly_yield']}</td>\n";
    echo "</tr>\n";
}

或者您可以使用您拥有的结果创建一个数组,然后打印该数组。这有效地将结果加在一起。所以我们假设数组的结构是这样的:

$results[Year-Quarter] = array(Year, Quarter, QuarterName, Result1, Result2)

然后你可以按如下方式构造数组:

$results = array();

while($row = mysql_fetch_array($result))
{
    $key = $row['Year'] . '-' . $row['Quarter'];
    $results[$key] = array(
        'Year'            => $row['Year'],
        'Quarter'         => $row['Quarter'],
        'Quarter_Name'    => $row['Quarter_Name'],
        'Quarter_yield_1' => $row['Quarter_yield'],
        'Quarter_yield_2' => 0,
    );
}

while($row = mysql_fetch_array($result8))
{
    $key = $row['Year'] . '-' . $row['Quarter'];

    // Check if we have this key
    if (isset($results[$key]))
    {
        $results[$key]['Quarter_yield_2'] = $row['Quarter_yield'];
    }
    else
    {
        $results[$key] = array(
            'Year'            => $row['Year'],
            'Quarter'         => $row['Quarter'],
            'Quarter_Name'    => $row['Quarter_Name'],
            'Quarter_yield_1' => 0,
            'Quarter_yield_2' => $row['Quarter_yield'],
        );
}

然后打印$results数组

的结果
foreach ($results as $item)
{
    echo "<tr id='centered' >\m";  
    echo "<td class='leftalign'>{$item['Quarter_Name']}</td>\n";
    echo "<td>{$item['Quarter_yield_1']}</td>\n";
    echo "<td>{$item['Quarter_yield_2']}</td>\n";
    echo "</tr>\n";
}