[英]How can I select two tables in MySQL but only select one column from the 2nd table?
I have two tables, one table is for users and stores everything about the user, but more importantly a users profile image stored as a file location. 我有两个表,一个表用于用户,并存储有关用户的所有内容,但更重要的是,用户配置文件图像存储为文件位置。 The second table is an images table, where images location and information is stored, such as title and description.
第二个表是图像表,其中存储了图像位置和信息,例如标题和描述。
I need to link the profile image to the user who uploaded the photo. 我需要将个人资料图片链接到上传照片的用户。 The users table has a user_id and profile_image, and the images table has just a user_id.
用户表具有user_id和profile_image,图像表仅具有user_id。
How can I select all of the images table and just the profile image in one query? 如何在一个查询中选择所有图像表而仅选择个人资料图像? Is this even possible?
这有可能吗?
Edit I have been looking at joins but can't seem to figure it out. 编辑我一直在寻找联接,但似乎无法弄清楚。 The tables look like this:
这些表如下所示:
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
I need profile_image to be selected so it can be called upon using $row[profile_image], but I also need all of the information from the images table. 我需要选择profile_image,以便可以使用$ row [profile_image]进行调用,但是我还需要images表中的所有信息。 Hopefully this clarifies things!
希望这可以澄清事情!
You can use a join based on user_id eg for select all the columns for the user_id = 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.