[英]How to retrieve data from a table and count number rows from another table then show them together in a while loop or HTML table?
I need to retrieve 'projects'
details from table 'projects'
. 我需要从表
'projects'
检索'projects'
详细信息。
I need to get number of bids from 'projectsBids'
. 我需要从
'projectsBids'
获得投标数量。
Then show them all in a HTML table based on project IDs 然后将它们全部显示在基于项目ID的HTML表格中
This is my 'projectsBids'
table: 这是我的
'projectsBids'
表:
pID | bidder
-----+----------------
AAAA | mark
AAAA | pete
AAAA | dave
BBBB | mason
BBBB | simon
Following code shows number of bids for a particular project ID (pID) 以下代码显示了特定项目ID(pID)的出价数量
<?php
$pID = $_GET["pID"];
$query = "SELECT pID FROM projectsBids WHERE pID='$pID'";
$result = mysqli_query($con, $query) or die('error');
echo mysqli_num_rows($result);
?>
Now I want to show this count using it's 'pID'
in a HTML summary table where I am retrieve data from another table 'projects'
现在,我想在HTML摘要表中使用它的
'pID'
来显示此计数,我将从另一个表'projects'
检索数据
'projects'
table: 'projects'
表:
pID | pName | pBudget
--------------------------------
AAAA | Create Website | 250
BBBB | Create an App | 550
I am using the following code to retrieve the data.. 我正在使用以下代码来检索数据。
Check 4th <td>
(in following code) where I want to display the number of bids for that project ID. 选中第4个
<td>
(在以下代码中),我要在其中显示该项目ID的出价数量。
<?php
$sql="SELECT pID,pName,pBudget FROM projects ORDER BY id";
$result=mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['pID']."</td>";
echo "<td>".$row['pName']."</td>";
echo "<td>".$row['pBudget']."</td>";
echo "<td>"**HERE I WANT TO SHOW 'Number of BIDS of PROJECT ID(pID)'**"</td>";
echo "</tr>";
}
?>
Whenever I try with subquery or join it does not work.. 每当我尝试使用子查询或联接时,它都无法正常工作。
What is the correct query to do this? 什么是正确的查询来做到这一点?
Try this I have tested it on your data. 试试这个,我已经对您的数据进行了测试。
SELECT *,( SELECT COUNT(pID) FROM projectsBids WHERE projectsBids.pID = projects.pID) as bids FROM projects use this query you will get the result. SELECT *,(SELECT COUNT(pID)FROM projectsBids WHERE projectsBids.pID = projects.pID)作为来自项目的投标,使用此查询您将获得结果。
尝试这个:
select p.pid,p.pname,p.pbudget,count(pb.bidder) as totbids from projects p left join projectsBids pb on p.pid=pb.pid group by p.pid,p.pname,p.pbudget
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.