开发者

trying to display results in from a JOIN mysql that are no present in one of the tables

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开发者_StackOverflow_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>";
}


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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜