PHP 运行 联合所有 SQL 语句

PHP running Union All SQL Statement

我试图将两个 table 组合在一起,但是每当我 运行 程序时,就会发生这种情况。

如您所见,我已经重复了 sql 声明。这是我的代码。

$queryc1 = "select sum(repeater),sum(membersigned) from sales UNION ALL select count(*) from approach;"; //DO INNERJOIN PRACTISE
$resultc1 = mysqli_query($dbconn,$queryc1);
echo "<table>
<tr>
<th>Repeater</th>
<th>Members</th>
<th>Approach</th>
</tr>";
while($row = mysqli_fetch_array($resultc1)) {
    echo "<tr>";
    echo "<td>" . $row['sum(repeater)'] . "</td>";
    echo "<td>" . $row['sum(membersigned)'] . "</td>";
    echo "<td>" . $row['count(*)'] . "</td>";
    echo "</tr>";
}
echo "</table>";
echo $queryc1;

我想像照片中那样显示计数 (*),作为生成的 table 的第三列。

当您将两个或多个查询合并在一起时,每个查询应该具有相同数据类型的相同数据列,例如:

SELECT SUM(repeater),SUM(membersigned) FROM sales
UNION
SELECT Text1,Text2 FROM approach

用这种格式替换您的查询

这里不需要UNION select,可以使用子查询:

select sum(repeater) as repeater_sum,
       sum(membersigned) as membersigned_sum,
       (select count(*) from approach) as approach_count
from sales;

在PHP中你使用$row['repeater_sum']..等