简体   繁体   中英

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM