与第一个表相比,计算第二个表中的出现次数

发布于 2024-10-17 23:45:19 字数 757 浏览 0 评论 0原文

我有两张表,一张保存我网站的贡献者信息,一张保存有关所贡献照片的信息。

对于网站的管理端,我想使用 php 和 mysql 创建一个表,显示所有贡献者,同时也计算每个贡献者可用于该网站的照片数量。

我使用此代码获取名称列表

  SELECT *  
    FROM site_con 
ORDER BY surn ASC

然后设置一个循环来列出所有名称,但在该循环中添加了一个查询以使用此代码计算照片的数量

$contributor = $row_rsContrib['con_Code'];

mysql_select_db($database_connGrowl, $connGrowl);
$query_rsCounter = "SELECT COUNT(*) AS Count 
                      FROM site_phts 
                     WHERE photter = $contributor";

$rsCounter = mysql_query($query_rsCounter, $connGrowl) or die(mysql_error());
$row_rsCounter = mysql_fetch_assoc($rsCounter);
$totalRows_rsCounter = mysql_num_rows($rsCounter);

唯一的问题是当“$contributor”不在中时照片表,它返回一个错误。

有什么想法吗?

I have two tables, one holds the information of contributors to my site and one holds information on photographs contributed.

For the admin side of the site, I want to create a table using php and mysql that displays all contributors but also counts the number of photographs each contributor has available for the site.

I get the list of names using this code

  SELECT *  
    FROM site_con 
ORDER BY surn ASC

I have then set up a loop to list all the names but have added a query within that loop to count the number of photographs using this code

$contributor = $row_rsContrib['con_Code'];

mysql_select_db($database_connGrowl, $connGrowl);
$query_rsCounter = "SELECT COUNT(*) AS Count 
                      FROM site_phts 
                     WHERE photter = $contributor";

$rsCounter = mysql_query($query_rsCounter, $connGrowl) or die(mysql_error());
$row_rsCounter = mysql_fetch_assoc($rsCounter);
$totalRows_rsCounter = mysql_num_rows($rsCounter);

The only problem is when '$contributor' is not in the photographs table, it returns an error.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

九厘米的零° 2024-10-24 23:45:19

您可以获得贡献者列表和单个查询中的照片数量:

   SELECT sc.*,
          COALESCE(x.numPhotos, 0) AS numPht
     FROM SITE_CON sc
LEFT JOIN (SELECT sp.photter,
                  COUNT(*) AS numPhotos
             FROM SITE_PHTS sp
         GROUP BY sp.photter) x ON x.photter = sc.con_code
 ORDER BY ssc.surn

您的查询失败,因为摄影师不一定有贡献 - 上面的查询返回摄影师列表,而那些没有关联照片的摄影师的 numPht 值为零。这是JOIN 入门 ,帮助解释正在使用的 OUTER JOIN。

You can get the list of contributors & the number of photos in a single query:

   SELECT sc.*,
          COALESCE(x.numPhotos, 0) AS numPht
     FROM SITE_CON sc
LEFT JOIN (SELECT sp.photter,
                  COUNT(*) AS numPhotos
             FROM SITE_PHTS sp
         GROUP BY sp.photter) x ON x.photter = sc.con_code
 ORDER BY ssc.surn

Your query fails because a photographer doesn't necessarily have contributions -- the query above returns the list of photographers, and those without photos associated will have a numPht value of zero. Here's a primer on JOINs, to help explain the OUTER JOIN that's being used.

画骨成沙 2024-10-24 23:45:19

实际上,最好的方法是使用 MSQL 而不是 PHP 进行计数:

SELECT site_con.*, COUNT( photo_id )
FROM site_con
LEFT JOIN site_phts ON site_con.con_Code = site_phts.photter
GROUP BY site_con.con_Code
ORDER BY site_con.surn

LEFT JOIN 具有特殊属性,当右表(照片)中没有与某个行匹配的行时,它会创建 NULL 条目。贡献者行。 COUNT 不会计算这些 NULL 条目。 (您需要照片表中有一些独特的列,我为此使用了 photo_id 。)

Actually the best way to do this is by using MSQL to count rather than PHP:

SELECT site_con.*, COUNT( photo_id )
FROM site_con
LEFT JOIN site_phts ON site_con.con_Code = site_phts.photter
GROUP BY site_con.con_Code
ORDER BY site_con.surn

The LEFT JOIN has the special property of creating NULL entries when there is no row in the right table (photos) that matches a contributor row. COUNT will not count these NULL entries. (You need some unique column in the photos table, I used photo_id for that.)

杀手六號 2024-10-24 23:45:19

这是贡献者和照片之间的关系:

  • 1 张照片最多可以有 1 个贡献者
  • 1 个贡献者可以有最多无限张照片

贡献者 <-(0,n)------(0,1)->拍摄照片,

因此您可能想在这两个表之间添加连接,我的意思是您将 con_id 添加到照片表(作为一列)。

这样您就可以在一个 SQL 查询中检索所有信息。

(就像 OMG 小马刚才说的)

this is the relation between Contributors and photographs:

  • 1 photograph can have a most 1 Contributor
  • 1 Contributor can have a most infinit photograph

Contributor <-(0,n)------(0,1)-> Photograph

so you might wanna add a connexion betweet those two tables, I mean you add the con_id to the photographs table (as a column).

this way you'll be able to retrieve all the informations in one SQL query.

(like OMG Ponies just said)

信愁 2024-10-24 23:45:19

做这样的事情,我相信这应该有效:

$result = mysql_query("SELECT COUNT(*) AS Count FROM site_phts WHERE photter = '$contributor'"); // put the single quote if $contributor is a string value

//use mysql_fetch_array

if ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %d", $row[0]);  
}

希望这有效,祝你好运,伙计!

Do something like this, I believe this should work :

$result = mysql_query("SELECT COUNT(*) AS Count FROM site_phts WHERE photter = '$contributor'"); // put the single quote if $contributor is a string value

//use mysql_fetch_array

if ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %d", $row[0]);  
}

Hopefully this works, Good luck mate !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文