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 8530539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:16:15+00:00 2026-06-11T09:16:15+00:00

I have two tables: USER: userID | uName | —————– 1 | John 2

  • 0

I have two tables:

USER:

userID | uName | 
-----------------
   1   |  John
   2   |  Bohn
   3   |  Kohn
   4   |  Lohn
   5   |  Zohn
   6   |  Rohn

IMAGES:

imageID | url | userID
----------------------
   1    |  x  |   2
   2    |  x  |   2
   3    |  x  |   1
   4    |  x  |   3
   5    |  x  |   3
   6    |  x  |   3

As you can see there is USER database which connects to IMAGES database by userID.
There is only one unique user row but there can be more than one image per user.

What i want is to fetch ONE RANDOM user from database and all of his images. Condition is that user should not be selected if he has no images.

Can you help me build ONE mysql query to achieve that?

P.S. It doesnt matter if user data duplicates (because of multiple image rows), there is only one user in resultset anyways.

  • 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-06-11T09:16:17+00:00Added an answer on June 11, 2026 at 9:16 am

    First, INNER JOIN both tables, to exclude users that don’t have any images; then, ORDER BY RAND(), to randomize the resultset; finally, select the first row to obtain a random userId:

    SELECT u.*
    FROM user u INNER JOIN images i ON (u.userId = i.userId)
    ORDER BY RAND()
    LIMIT 1
    

    DEMO.

    EDIT: To select all images from a random user, use the following:

    SELECT i.*
    FROM images i 
         INNER JOIN 
         (SELECT u.userId
          FROM user u INNER JOIN images i ON (u.userId = i.userId)
          ORDER BY RAND()
          LIMIT 1) rand_user 
         ON (i.userId = rand_user.userId);
    

    DEMO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have four tables user-question contains two columns: questionID, userID, the questions that the
I have two tables, in which one user can have several contacts: User(UserId, UserName)
I have two tables like User and UserInfo UsersInfo UserId UserName Email Password LastLoginDate
I have two tables: tblBadge ------- ID Name tblBadgeUsers ------- BadgeID UserID A user
I have two tables in my database who looks like this: User: userid(int) username(varchar)
If i have two tables like user table-u userid | name 1 | lenova
I am facing a situation and need help. I have two tables: user: user_id
I have two tables user table user_id | name | 1 | peter |
I have two tables, a user table and a application table: User id username
I have (again) this tables: User: id | name ------------- 1 | 'John' 2

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.