我有两个表,我想比较并找到用户名不符合的用户,然后显示未被跟踪的用户的用户名。SQL从2个表中获取信息并比较
Edit:
Table 1: users
username
Table 2: follow
username (the name of the user)
followname (the name of the person the user follows)
In Table 2 username and followname are never null. When a user follows another user then it is put into the list like so:
Username Followname
derekshull dvdowns
derekshull testuser
dvdowns testuser
testuser 1511project
derekshull newuser
在我脑子里,我见表1获得所有用户名的列表(列名是“用户名”,从表1),然后得到所有的用户名的用户($用户名)的列表不继(表2中的列名是“followname”)。
如何让它比较两个列表并且不显示用户已经遵循的用户名?
这就是我现在所拥有的。出于某种原因,它正在显示我遵循并且不遵循的用户。
$alluserslookup = mysql_query("SELECT * FROM users WHERE username!='$username'");
$thefollowerslookup = mysql_query("SELECT * FROM follow WHERE username='$username'");
while ($followersrow = mysql_fetch_assoc($thefollowerslookup)) {
$afollower = $followersrow['followname'];
while ($allusersrow = mysql_fetch_assoc($alluserslookup)) {
$allusers = $allusersrow['username'];
if ($afollower != $allusers) {
echo "<a href='viewprofile.php?viewusername=$allusers'>$allusers</a><br>";
}
}
}
您是否听说过['LEFT JOIN'](http://google.com/?q=LEFT+JOIN)或['JOIN'](http://google.com/?q=SQL+JOIN )本身? – shadyyx 2013-04-24 15:56:57
我没有。这将如何帮助? – derekshull 2013-04-24 15:59:06
请参阅http://dev.mysql.com/doc/refman/5.0/en/join.html,了解如何在MySQL中使用它,以及我如何在这种情况下使用它的例子。 。 (你在使用MySQL吗?) – 2013-04-24 16:02:41