简体   繁体   English

如何在MySQL中选择两个表,但仅从第二个表中选择一列?

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

相关问题 我如何从第一选择查询中获取两个ID,然后将其用于第二选择查询? - how can i get two id from 1st select query and then use this to 2nd select query? mysql从2个表中选择数据,但第二个表具有动态单元格名称 - mysql select data from 2 tables, but 2nd table has dynamic cell names 如何在同一选择语句中从第一张表的列值中选择第二张表的数据? - How to select data of 2nd table from column values of 1st table in same select statement? 如何从由第三个表连接的两个mySQL表中选择数据? - How can I select data from two mySQL tables connected by a third table? 如何在一个MySQL查询中从不同的表中进行选择? - How can I select from different tables in one MySQL query? MySQL从两个表中选择,其中一列包含2个变量 - MySQL select from two tables where one column contain 2 variables 如何从两个表中进行选择,并且仅显示一个表中的一项,而第二个表中的多项呢? - How to select from two tables and display only one item from 1 table and many from the second table? PHP无法仅从mysql表之一选择任何内容 - PHP can't select anything from only one of the mysql tables mysql从表中全选,但只更改一个列名 - mysql select all from table but change column name of only one MySql 查询连接两个表以从参考中的第二个表中获取结果 - MySql query for Joining two tables to get result from 2nd table in reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM