PHP While 循环未运行 MySQL 数据库中所有选定的记录

发布于 2024-11-28 06:29:59 字数 1387 浏览 0 评论 0 原文

我的博客下面有三张表:一张用于用户,一张用于评论,一张用于主题。 并非所有检索到的记录都会由 PHP 回显,我认为这可能与 NULL 值有关,例如在我的会员列表页面上,以下查询检索四个用户的数据:

SELECT COUNT(Topics.MemberID) AS NumberOfTopics, Users.id, 
    Users.FirstName, Users.LastName, Users.Joined 
FROM Users LEFT JOIN Topics ON Users.id=Topics.MemberID GROUP BY Topics.MemberID

但是,如果列表末尾的最后一个用户没有主题 (NumberOfTopics = 0) 它们不会被呈现。 如果用户的帖子计数为 0,但他们关注的记录的帖子计数不为 0,则会显示该用户。为什么?

这是完整的代码:

$result = mysql_query("SELECT COUNT(Topics.MemberID) AS NumberOfTopics, ".
                      "Users.id, Users.FirstName, Users.LastName, Users.Joined ".
                      "FROM Users LEFT JOIN Topics ON Users.id=Topics.MemberID ".
                      "GROUP BY Topics.MemberID") 
             or die("Query failed with error: ".mysql_error());

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Joined</th>
<th>No. of posts</th>
</tr>";

while($row = mysql_fetch_assoc($result))
  {
  echo "<tr>";
  echo "<td><a href='/neuro/profile.php?userid=$row[id]'>" . 
       $row['FirstName'] . " " . $row['LastName'] . "</a></td>";
  echo "<td>" . $row['Joined'] . "</td>";
  echo "<td>" . $row['NumberOfTopics'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

感谢您的阅读!

I have three tables underlying a blog: one for users, one for comments and one for threads.
Not all the records retrieved are echoed by PHP and I think it may be to do with NULL values, for example on my memberlist page the following query retrieves data on four users:

SELECT COUNT(Topics.MemberID) AS NumberOfTopics, Users.id, 
    Users.FirstName, Users.LastName, Users.Joined 
FROM Users LEFT JOIN Topics ON Users.id=Topics.MemberID GROUP BY Topics.MemberID

However if the last user(s) at the end of the list has no topics (NumberOfTopics = 0) they are not presented.
If a user has a post count of 0 but they following record has a post count that isn't 0 the user is shown. Why?

Here is the complete code:

$result = mysql_query("SELECT COUNT(Topics.MemberID) AS NumberOfTopics, ".
                      "Users.id, Users.FirstName, Users.LastName, Users.Joined ".
                      "FROM Users LEFT JOIN Topics ON Users.id=Topics.MemberID ".
                      "GROUP BY Topics.MemberID") 
             or die("Query failed with error: ".mysql_error());

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Joined</th>
<th>No. of posts</th>
</tr>";

while($row = mysql_fetch_assoc($result))
  {
  echo "<tr>";
  echo "<td><a href='/neuro/profile.php?userid=$row[id]'>" . 
       $row['FirstName'] . " " . $row['LastName'] . "</a></td>";
  echo "<td>" . $row['Joined'] . "</td>";
  echo "<td>" . $row['NumberOfTopics'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

Thanks for reading!

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

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

发布评论

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

评论(4

若沐 2024-12-05 06:29:59

我相信问题在于您的 GROUP BY 将强制消除 NULL 。

我通常会在这些情况下使用子查询,因为它更容易阅读和理解:

SELECT (select count(*) from Topics T where T.MemberID = U.id) AS NumberOfTopics,
    Users.id, Users.FirstName, Users.LastName, Users.Joined 
FROM Users u

I believe the problem is that your GROUP BY will force the elimination of NULLs.

I normally will use subquery in these circumstances simply because it is easier to read and understand:

SELECT (select count(*) from Topics T where T.MemberID = U.id) AS NumberOfTopics,
    Users.id, Users.FirstName, Users.LastName, Users.Joined 
FROM Users u
梦里南柯 2024-12-05 06:29:59

我想这是因为你的group by。

尝试:

select
    topics.count,
    users.id,
    users.firstname,
    users.lastname,
    users.joined
from
    users left join
    (
        select
            topics.memberid,
            count(*) count
        from
            topics
        group by
            topics.memberid
    ) topics on users.id = topics.memberid

这应该收集您在子查询中查找的主题计数,然后将这些计数与所有用户进行左连接。

I think it's because of your group by.

Try:

select
    topics.count,
    users.id,
    users.firstname,
    users.lastname,
    users.joined
from
    users left join
    (
        select
            topics.memberid,
            count(*) count
        from
            topics
        group by
            topics.memberid
    ) topics on users.id = topics.memberid

This should gather the count of topics that you're after in a sub query and then left join those counts against all users.

送你一个梦 2024-12-05 06:29:59

你的询问有些矛盾。 LEFT JOIN 将返回左表中的所有行,即使右表中没有匹配项。然而,在您的查询中,您将按 Topics.MemberID 对记录进行分组。对于没有发帖的用户,他们将不会显示任何记录!尝试以下查询:

SELECT 
COUNT(Topics.MemberID) AS NumberOfTopics, Users.id, 
Users.FirstName, Users.LastName, Users.Joined 
FROM Users LEFT JOIN Topics ON Users.id=Topics.MemberID 
GROUP BY Users.id

Your query is somewhat contradictory. LEFT JOIN will return all rows from the left table, even if there are no matches in the right table. HOWEVER in the case of your query you are grouping your records by Topics.MemberID. For users with no post their will be no record shown!! Try the following query:

SELECT 
COUNT(Topics.MemberID) AS NumberOfTopics, Users.id, 
Users.FirstName, Users.LastName, Users.Joined 
FROM Users LEFT JOIN Topics ON Users.id=Topics.MemberID 
GROUP BY Users.id
衣神在巴黎 2024-12-05 06:29:59

您的查询似乎不正确。
我会使用内部联接

SELECT COUNT(*) AS NumberOfTopics
  , Users.id
  , Users.FirstName
  , Users.LastName
  , Users.Joined 
FROM Users 
INNER JOIN Topics ON Users.id=Topics.MemberID 
GROUP BY Topics.MemberID

Your query seems incorrect.
I'd use an inner join

SELECT COUNT(*) AS NumberOfTopics
  , Users.id
  , Users.FirstName
  , Users.LastName
  , Users.Joined 
FROM Users 
INNER JOIN Topics ON Users.id=Topics.MemberID 
GROUP BY Topics.MemberID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文