我需要运行两个语句来返回我需要的数据。下面的代码将返回除了可以从不同表中检索的count列之外的所有需要的代码。
如何在同一代码中运行这两个语句以检索计数?
以下是我的代码:
<?php
$query = "SELECT * from $CLIENTS_DATABASE order by id DESC";
if ($result = mysqli_query($conn, $query)) {
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row["name"]; ?> </td>
<td><?php echo $row["url"]; ?> </td>
<td><?php echo $row["secret"]; ?> </td>
<td><?php echo $row["count"]; ?> </td>
</tr>
<?php
}
mysqli_free_result($result);
}
?>
我需要的计数数据的另一个陈述是:
$query = "SELECT COUNT(*) as count from visits where id_client='$result[id]'"
感谢您的时间和任何帮助。
答案 0 :(得分:0)
加入两个查询。
$query = "SELECT order.*, IFNULL(c.count, 0) AS count
FROM $CLIENTS_DATABASE AS order
LEFT JOIN (SELECT id_client, COUNT(*) AS count
FROM visits
GROUP BY id_client) AS c
ON c.id_client = order.id
ORDER BY order.id DESC";