简体   繁体   English

将MySQL Count列添加到PHP / HTML表

[英]Add MySQL Count Column to PHP/HTML Table

I'm developing a website using HTML, PHP and MySQL to access a database. 我正在使用HTML,PHP和MySQL开发一个网站来访问数据库。 On one page I present a table with data from that database. 在一页上,我提供了一个表,其中包含来自该数据库的数据。 This is some of the code I'm using: 这是我正在使用的一些代码:

$sql1 = "SELECT * FROM MyTable ORDER BY ID ASC";
$rs1 = mysqli_query($link,$sql1);
(...)
while($row1 = mysqli_fetch_assoc($rs1)) {
  echo "<tr><td>".$row1['ID']."</td><td>".$row1['Field1']."</td><td></td><td>".$row1['Field2']."</td><td>".$row1['Field3']."</td></tr>\n" ;
}

Notice the empty <td></td> ? 注意空的<td></td>吗? That's because I want to have there the number of time a given ID appears on two other tables (there are foreign keys involved, obviously). 那是因为我想让给定ID出现在其他两个表上的次数(显然,其中涉及外键)。 I have sorted out the code I need for that: 我已经整理出我需要的代码:

$sql2 = "SELECT (SELECT COUNT(*) FROM MyTable2 WHERE ID2=$row1['ID'])+(SELECT COUNT(*) FROM MyTable3 WHERE ID2=$row1['ID']) AS total";

However, I'm struggling with figuring out a way to add this result to the other table. 但是,我正在努力寻找一种将结果添加到另一个表的方法。 Any help? 有什么帮助吗?

try with this.. it inserts the total to an table after selecting the count. 尝试使用此方法..它在选择计数后将总数插入表中。

"INSERT INTO total_table (total)
 SELECT (SELECT COUNT(*) FROM MyTable2 WHERE ID2=$row1['ID'])+(SELECT COUNT(*) FROM MyTable3 WHERE ID2=$row1['ID']) AS total
 WHERE  cid = 2"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM