我有2个表,我必须使用where =“此值在其他表中”来获取一个表的数据
users_info and users_frnds
users_info look like this
name image presently id
somename somimage studying 2
somename somimage studying 3
users_frnds table looks like this
userid friendid
1 2
1 3
$query = "SELECT * FROM users_info WHERE users_info.id =
users_frnds.friendid";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['image'];
echo "<br />";
但它似乎在这里不起作用。我想一次将所有数据放入阵列。
谁能帮我这个忙。谢谢
它抛出了这个错误
Unknown column 'users_frnds.friendid' in 'where clause'
最佳答案
您将需要加入,例如:
SELECT specifyfields
FROM users_friends
INNER JOIN users_info ON users_info.id=users_friends.friendid
然后,您将获得访问权限,并尝试从不在WHERE子句中联接表,因为这会创建难以理解的查询。