与第一个表相比,计算第二个表中的出现次数
我有两张表,一张保存我网站的贡献者信息,一张保存有关所贡献照片的信息。
对于网站的管理端,我想使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以获得贡献者列表和单个查询中的照片数量:
您的查询失败,因为摄影师不一定有贡献 - 上面的查询返回摄影师列表,而那些没有关联照片的摄影师的 numPht 值为零。这是JOIN 入门 ,帮助解释正在使用的 OUTER JOIN。
You can get the list of contributors & the number of photos in a single query:
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.
实际上,最好的方法是使用 MSQL 而不是 PHP 进行计数:
LEFT JOIN
具有特殊属性,当右表(照片)中没有与某个行匹配的行时,它会创建 NULL 条目。贡献者行。COUNT
不会计算这些 NULL 条目。 (您需要照片表中有一些独特的列,我为此使用了 photo_id 。)Actually the best way to do this is by using MSQL to count rather than PHP:
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.)这是贡献者和照片之间的关系:
贡献者 <-(0,n)------(0,1)->拍摄照片,
因此您可能想在这两个表之间添加连接,我的意思是您将 con_id 添加到照片表(作为一列)。
这样您就可以在一个 SQL 查询中检索所有信息。
(就像 OMG 小马刚才说的)
this is the relation between Contributors and photographs:
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)
做这样的事情,我相信这应该有效:
希望这有效,祝你好运,伙计!
Do something like this, I believe this should work :
Hopefully this works, Good luck mate !