【问题标题】:How can I select two tables in MySQL but only select one column from the 2nd table?如何在 MySQL 中选择两个表,但只从第二个表中选择一列?
【发布时间】:2017-05-06 19:14:58
【问题描述】:

我有两张表,一张用于用户并存储有关用户的所有信息,但更重要的是存储为文件位置的用户个人资料图像。第二个表是图像表,其中存储图像位置和信息,例如标题和描述。

我需要将个人资料图片链接到上传照片的用户。 users 表有一个 user_id 和 profile_image,而 images 表只有一个 user_id。

如何在一个查询中选择所有图片表和个人资料图片?这甚至可能吗?

编辑 我一直在寻找连接,但似乎无法弄清楚。表格如下所示:

IMAGES                             USERS
id                                 user_id
user_id                            username
username                           password
title                              isadmin
image (location)                   points
description                        signup_date
points                             email
category                           city
                                   bio
                                   profile_image

我需要选择 profile_image 以便可以使用 $row[profile_image] 调用它,但我还需要 images 表中的所有信息。希望这可以澄清事情!

【问题讨论】:

标签: php mysql


【解决方案1】:

您可以使用基于 user_id 的连接 例如选择 user_id = 1 的所有列

  select USERS.* , IMAGES.*
  from USERS
  INNER JOIN IMAGES ON USERS.user_id = IMAGES.user_id
  WHERE USERS.user_id = 1

【讨论】:

  • 抱歉,耽搁了这么久,这不是我所需要的。虽然它确实帮助我尝试解决其他问题,但无济于事。我需要能够从 users 表中调用 $row[profile_image] ,但只能从 id 匹配的图像中选择信息。除非我很傻,否则使用您的答案似乎没有这样做?
  • 然后如前所述..更新您的问题添加适当的数据样本和预期的..结果..
猜你喜欢
  • 1970-01-01
  • 2012-09-13
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多