我在我的c#app上尝试过相同的代码并且工作正常,但是当我在php上尝试使用相同的代码而不是c#上的列号时,我将其替换为列名,但结果却不同。
$sql ="SELECT
`adexposure`.`symbol`,
`adexposure`.`netvolume`,
`fxexposure`.`netvolume`,
`adexposure`.`lastupdate`
FROM `adexposure`
LEFT JOIN `fxexposure` ON `adexposure`.`symbol` = `fxexposure`.`symbol`";
$result = $conn->query($sql);
if($result->num_rows>0)
{
echo "<table>";
echo "<tr>";
echo "<th>Symbol</th>";
echo "<th>Net Volume A</th>";
echo "<th>Net Volume B</th>";
echo "<th>Last Update</th>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" .$row["symbol"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["lastupdate"]."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "0 results";
}
结果应该是:
<table border=1>
<tr>
<th>Symbol</th>
<th>Net Volume A</th>
<th>Net Volume B</th>
<th>Last Update</th>
</th>
</tr>
<tr>
<td>BITCOIN</td>
<td>2.5</td>
<td>3.5</td>
<td>2018.02.05 10:44</td>
</tr>
<tr>
<td>LITECOIN</td>
<td>1.5</td>
<td>5.5</td>
<td>2018.02.05 10:44</td>
</tr>
<tr>
<td>HASHCOIN</td>
<td>0.5</td>
<td>0.5</td>
<td>2018.02.05 10:44</td>
</tr>
</table>
&#13;
但事实并非如此。 结果显示fxexposure.netvolume的净容量。
我希望你能帮助我。
...谢谢
答案 0 :(得分:3)
只是建议你有两次netvolumn所以你应该使用别名
sql ="SELECT
`adexposure`.`symbol`,
`adexposure`.`netvolume`,
`fxexposure`.`netvolume` as netvolume2,
`adexposure`.`lastupdate`
FROM `adexposure`
LEFT JOIN `fxexposure` ON `adexposure`.`symbol` = `fxexposure`.`symbol`";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" .$row["symbol"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["netvolume2"]."</td>";
echo "<td>" .$row["lastupdate"]."</td>";
echo "</tr>";
}
答案 1 :(得分:0)
如果您为返回的列分配alias
,则可以在php中正确地解决它们
$sql ="SELECT
`adexposure`.`symbol`,
`adexposure`.`netvolume` as `netvolume_A`, /* ALIAS */
`fxexposure`.`netvolume` as `netvolume_B`,
`adexposure`.`lastupdate`
FROM `adexposure`
LEFT JOIN `fxexposure` ON `adexposure`.`symbol` = `fxexposure`.`symbol`";
$result = $conn->query($sql);
if($result->num_rows>0)
{
echo "<table>";
echo "<tr>";
echo "<th>Symbol</th>";
echo "<th>Net Volume A</th>";
echo "<th>Net Volume B</th>";
echo "<th>Last Update</th>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" .$row["symbol"]."</td>";
echo "<td>" .$row["netvolume_A"]."</td>";<!-- /* ALIAS */ -->
echo "<td>" .$row["netvolume_B"]."</td>";
echo "<td>" .$row["lastupdate"]."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "0 results";
}
答案 2 :(得分:0)
仔细阅读以下两行:
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
你使用相同的名称来表示两个不同的东西,因此PHP无法在这些行上给出不同的结果。
解决方案是在SQL中为值提供唯一的列别名:
`adexposure`.`netvolume` as netvolume_as,
`fxexposure`.`netvolume` as netvolume_fx,
然后这些是你PHP中的名字:
echo "<td>" .$row["netvolume_as"]."</td>";
echo "<td>" .$row["netvolume_fx"]."</td>";