niko
niko

Reputation: 9393

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'

Upvotes: 0

Views: 67

Answers (1)

Luc Franken
Luc Franken

Reputation: 3014

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.

Upvotes: 1

Related Questions