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

发布于 2025-01-01 07:41:08 字数 738 浏览 0 评论 0原文

我有 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'

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰ツ都不明白 2025-01-08 07:41:08

您将需要联接,例如:

SELECT specifyfields 
FROM users_friends 
  INNER JOIN users_info ON users_info.id=users_friends.friendid

然后您将获得访问权限,尽量不要在 WHERE 子句中联接表,因为这可能会创建非常不可读的查询。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文