尝试显示 JOIN mysql 中不存在于表之一中的结果

发布于 2024-12-07 19:17:35 字数 964 浏览 0 评论 0原文

我有两个表,a_users 和 a_student_tutors。我基本上是想通过他们的身份证号码加入这两个人。但是,我试图找出不在 a_student_tutors 中的成员并显示它们。

这是例如编码

$queryone = "SELECT * from a_users JOIN a_student_tutors on a_users.id=a_student_tutors.mem_id ";

$resultone = mysql_query($queryone);

while ($row=mysql_fetch_assoc($resultone)){
$member = $row['mem_id'];

$query = "SELECT * FROM a_users where id != '$member'";
echo $query;

}
$result = mysql_query($query);
$numrows = mysql_num_rows($result);

while ($row=mysql_fetch_assoc($result)){
    echo "<tr>
          <td><a href=\"members-edit.php?id=".$row['id']."\">".$row['name']."</a></td>
          <td>".$row['username']."</td>
          <td>".$row['address']." | ".$row['city']." | ".$row['postcode']."</td>
           <td ><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\" value=\"".$row['id']."\" /></td>
          </tr>";
}

I have two tables, a_users and a_student_tutors. I am basically looking to join the two on their id numbers. However, I am trying to find out the members who are not in the a_student_tutors and display them.

here is coding for example

$queryone = "SELECT * from a_users JOIN a_student_tutors on a_users.id=a_student_tutors.mem_id ";

$resultone = mysql_query($queryone);

while ($row=mysql_fetch_assoc($resultone)){
$member = $row['mem_id'];

$query = "SELECT * FROM a_users where id != '$member'";
echo $query;

}
$result = mysql_query($query);
$numrows = mysql_num_rows($result);

while ($row=mysql_fetch_assoc($result)){
    echo "<tr>
          <td><a href=\"members-edit.php?id=".$row['id']."\">".$row['name']."</a></td>
          <td>".$row['username']."</td>
          <td>".$row['address']." | ".$row['city']." | ".$row['postcode']."</td>
           <td ><input type=\"checkbox\" name=\"checkbox[]\" id=\"checkbox[]\" value=\"".$row['id']."\" /></td>
          </tr>";
}

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

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

发布评论

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

评论(3

空城旧梦 2024-12-14 19:17:36

您还可以使用左连接,如下所示,我知道这些工作非常好,在过去几年中大量使用了它们。

SELECT a_users.* FROM a_users
LEFT JOIN a_student_tutors
    ON a_users.id = a_student_tutors.mem_id
WHERE a_student_tutors.mem_id IS NULL

You can also use a left join as follows, I know these work extremely well, having used them a great deal over the last few years.

SELECT a_users.* FROM a_users
LEFT JOIN a_student_tutors
    ON a_users.id = a_student_tutors.mem_id
WHERE a_student_tutors.mem_id IS NULL
任性一次 2024-12-14 19:17:36

子选择会帮助你。

SELECT * FROM a_users 
WHERE id NOT IN (SELECT a_student_tutors.mem_id);

请小心子选择,因为这些是硬查询,可能会降低性能。

Subselect will help you.

SELECT * FROM a_users 
WHERE id NOT IN (SELECT a_student_tutors.mem_id);

Be carefull with subselects as these are hard queries which may reduce performanse.

云醉月微眠 2024-12-14 19:17:36

您应该使用 LEFT JOIN 而不是 INNER JOIN (JOIN)

You should use LEFT JOIN instead of INNER JOIN (JOIN)

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