简体   繁体   English

从一个表中选择行,但语句条件在另一个表中

[英]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.

相关问题 从一个表中选择表行,其中另一个表中的表列的值为x - Selecting table rows from one table where value of table column in another table is x 来自一个表的SQL COUNT,其中来自另一个表的条件为真 - SQL COUNT from one table where condition from another table is true 从一个条件为的表中检索数据,并在一个SELECT查询中检查另一个表中的记录 - Retrieve data from a table with a condition where and check the record in another table in one SELECT query Mysql查询选择其中一个表中的总和与另一个表中的行总和进行比较 - Mysql Query select where the sum from one table is compared to the sum of rows in another table 根据where子句从表中选择一列,然后将其与另一个表中的另一列匹配 - Pick a column from a table depending on a where clause and then match that to another column in another table 从表中选择其他表中的行不匹配的表 - select from table where rows from another table do not match MySQL:将一个表中的行替换为另一表中的行 - MySQL: Replace rows in one table with rows from another table 从一个表删除行并添加到另一个表 - Deleting rows from one table and adding to another 将行从一个表复制到另一个表时出现问题 - Issue in copying rows from one table to another 将行列表从一个表转移到另一个表 - Trasferring a list of rows from one table to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM