gpt4 book ai didi

php - 连接两个 mysql_fetch_array

转载 作者:行者123 更新时间:2023-11-29 14:09:35 25 4
gpt4 key购买 nike

我正在尝试将两个查询拼凑在一起。下面是我正在使用的代码。然而,该表正在分割数据。我该如何补救?或者有什么更好的解决方案?

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>";
}

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

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
)

最佳答案

在上面的代码中,您在第一个循环中打开 tr 标记,但从未关闭它。同样,在第一个循环中,您打印了三次季度 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";
}

关于php - 连接两个 mysql_fetch_array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13734099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com