Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7004321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:11:02+00:00 2026-05-27T21:11:02+00:00

I feel like there is a better way to do this. I’m mostly just

  • 0

I feel like there is a better way to do this. I’m mostly just asking if what I’m doing is right, or if there’s a much better way.

Observe these tables:

Users
id | name
---+---------
 1 | Andrew
 2 | Greg

Images
id | userid | url
---+--------+-------
 1 |   1    |  img/foo.jpg
 2 |   2    |  img/bar.jpg
 3 |   2    |  img/baz.jpg
 4 |   1    |  img/qux.jpg

If the Users table had a single foreign key that references Images, each user could only be associated with a single image. However, I’d like to allow each user to be associated with any number of images, hence putting a Users foreign key in Images instead.

I understand how to use JOIN if user has a single foreign key to data stored in another table. However, in this case, there is another table with a number of foreign keys associated with the user I’m interested in. How would I construct a SELECT query that will retrieve all of the images associated with the user in addition to all of the data associated with the user (user type, phone number, blah blah blah [I know how to do this already]) in one SELECT query?

Or, is there a better way to do this?

Edit 1:

For example, if you select the user with id = 1 then it would return the first row of Users and the first and last row of Images (because those rows have userid = 1).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:11:03+00:00Added an answer on May 27, 2026 at 9:11 pm

    If you do not want multiple rows with identical (user) data you can either group per user and use the GROUP_CONCAT() function to gather all the urls in one column:

    SELECT u.*
         , GROUP_CONCAT(i.URL)
    FROM users u
      LEFT JOIN Images i 
        ON u.ID = i.userID
    WHERE u.ID = 1
    GROUP BY u.ID
    

    or use 2 queries:

    SELECT u.*
    FROM users u
    WHERE u.ID = 1
    
    SELECT i.URL
    FROM Images i 
    WHERE i.userID = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like there has got to be a better way of doing this
Is there a better way to do this? I feel like I am doing
Is there a better way of writing this code? It just doesn't sit right
Is there a better way to write this? I feel like I'm getting rusty
I feel like there must be a much better way of expressing the following
OK, this is working but I feel there is a better way to do
I feel like there's a simple solution to this, but I'm not really sure
Sorry for the simple question but I feel like there's a smarter way to
I just had the jQuery epiphany the other day and still feel like there
I feel like I should know this, but I haven't been able to figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.