[英]Pick rows from one table but where statement condition is in another table
I have 2 tables, I have to get the data of one table using where = "this value in some other table"我有 2 个表,我必须使用 where = "this value in some other table" 来获取一个表的数据
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 />";
but it does not seem to work here.但它似乎在这里不起作用。 I wanted to get all the data at once into my array.
我想一次将所有数据放入我的数组中。
It throws me this error:它抛出了我这个错误:
Unknown column 'users_frnds.friendid' in 'where clause'
You will need to join, like:您需要加入,例如:
SELECT specifyfields
FROM users_friends
INNER JOIN users_info ON users_info.id=users_friends.friendid
Then you will get access, try never to join tables in the WHERE clause because that can create quite unreadable queries.然后您将获得访问权限,尽量不要在 WHERE 子句中连接表,因为这会创建非常不可读的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.