简体   繁体   English

如何在同一 SQL 查询期间计算不同表中的行数

[英]How to count rows from a different table during same SQL query

I would like to get all the jobs for a person, and count all the bids he received which is stored in a different table.我想获得一个人的所有工作,并计算他收到的所有出价,这些出价存储在不同的表中。

I have one table with following rows: table jobs: jobId,userid,title我有一个包含以下行的table jobs: jobId,userid,titletable jobs: jobId,userid,title

And a different table with the following: table bids: jobId,amount还有一个不同的表格,其中包含以下内容: table bids: jobId,amount

Now I want to get all the jobs from the first table jobs WHERE userid=1 and combine this with counting all rows in the second table table bids that have the same jobId for each job (line) that is found from the first table.现在我想从第一台获得所有的工作jobs WHERE userid=1 ,并在第二个表计数中的所有行结合本table bids具有相同jobId对于从第一个表中找到的每个作业(线)。

A possible output could be:可能的输出可能是:

job.jobId        job.userid       job.title       bids.Total
 1                  10            "My job"            20
 2                  11            "Other job"         5 

I know how to do it the wrong way , which is like this:我知道如何以错误的方式做到这一点,就像这样:

$stmt0 = $mysqli->stmt_init();
$stmt0->prepare("SELECT jobId,title FROM jobs WHERE userid=?");
$stmt0->bind_param('i', $userid);
$stmt0->execute();
$stmt0->bind_result($jobId,$title);

// Fetch the result of the query
while($stmt0->fetch()) { 

$ary = "SELECT amount FROM bids WHERE jobId='$jobId'";
if ($stmt_1 = mysqli_prepare($mysqli, $ary)) {
    mysqli_stmt_execute($stmt_1);
    mysqli_stmt_store_result($stmt_1);
    $total_bids = mysqli_stmt_num_rows($stmt_1);
    mysqli_stmt_close($stmt_1);
}

// show all jobs with total bids
...

 }


$stmt0->close();

How can I do this with one query?我怎样才能用一个查询来做到这一点?

You could use a Join a count and group by您可以使用 Join a count and group by

     SELECT a.jobId,a.userid, a.title, count(b.jobid) as bidsTotal 
     FROM jobs as a
     inner join bids as b on b.jobId = a.JobId
     WHERE userid=?
     Group by  a.jobId,a.userid, a.title

You could emulate the current "wrong way" approach in a single query, using correlated subquery in the SELECT list, so you would have just one query:您可以在单个查询中模拟当前的“错误方式”方法,使用 SELECT 列表中的相关子查询,因此您将只有一个查询:

 SELECT j.jobId
      , j.title
      , j.userid
      , ( SELECT COUNT(*) 
            FROM bids b 
           WHERE b.jobId = j.jobId
        ) AS cnt_bids
   FROM jobs j
  WHERE j.userid = ?
  ORDER BY j.jobId

The correlated subquery will get executed for every row returned by the outer query.相关子查询将针对外部查询返回的每一行执行。 This approach gets expensive if the outer query returns a large number of rows.如果外部查询返回大量行,这种方法会变得昂贵。

If there is a column (or set of columns) that are UNIQUE in the jobs table, we can get an equivalent result using an OUTER JOIN operation and a GROUP BY.如果jobs表中有一列(或一组列)是 UNIQUE,我们可以使用 OUTER JOIN 操作和 GROUP BY 获得等效的结果。

If we have a guarantee that jobId is UNIQUE in the jobs table, an equivalent result can be obtained using an outer join operation如果我们保证jobIdjobs表中是UNIQUE,则可以使用外连接操作获得等效的结果

 SELECT j.jobId
      , j.title
      , j.userid
      , COUNT(b.jobId) AS cnt_bids
   FROM jobs j
   LEFT
   JOIN bids b
     ON b.jobId = j.jobId
  WHERE j.userid = ?
 GROUP BY j.jobId, j.title, j.userid 
 ORDER BY j.jobId

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

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