简体   繁体   English

如何从一个表中检索数据并计算另一个表中的行数,然后在while循环或HTML表中一起显示它们?

[英]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.

相关问题 从mysql检索数据并显示在html表中 - Retrieve data from mysql and show in html table 从表中检索所有数据,然后计算另一个中的结果数 - Retrieve all data from table, then count how many results there are in another 如何从 MySQL 中的另一个表中检索数据,同时从第一个表的列中获取数据? - How to retrieve data from another table in MySQL while having a data from column from the first table? 计算单个查询中另一个表的行数 - Count number of rows from another table in single query 使 HTML 行可单击,然后显示来自数据库的行用户配置文件,用于使用 while 循环创建的表 - Make HTML row clickable then show that rows users profile from database, for a table that was created with a while loop 如何从另一个表的while循环内的一个表中获取数据? - How to get data from a table inside a while loop from another table? 在正在从另一个表中恢复其他数据的循环中,从相关表中检索数据 - retrieve data from a related table in a loop that is recovering other data from another table 计算另一个连接表中的行数 - count rows from another join table 如何从mysql将行检索到php / html表中 - How do I retrieve rows from mysql into a php/html table 如何从mysql php检索数据并在下拉列表中显示 - How to retrieve data from mysql php and show in table from dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM