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

  • Home
  • SEARCH
  • 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 8777693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:20:18+00:00 2026-06-13T19:20:18+00:00

I have a database which holds information about user-created items. Each item can have

  • 0

I have a database which holds information about user-created items. Each item can have a number of images associated with it, and I’d like to have an SQL query that 4 random item images, but should not return images from the same item twice.

Here’s a review of the table structures (not full, but including the pertinent information):

Table 1 : item_images:

+----------+--------------+
| item_id  |   filename   |
+----------+--------------+
|    1     |   test.jpg   | 
|    1     |   test2.jpg  |
|    5     |   testy.jpg  |
+----------+--------------+

Table 2: item_link:

+----------+-------------+
| item_id  | category_id |
+----------+-------------+
|    1     |      1      |
|    5     |      1      |
+----------+-------------+

So given the above data, I’d like to return a random image for item # 1 and 5, since they both exist in category # 1.

Here’s the SQL query I have already tried, but for some reason, it always returns the same two images. How might I modify this to return a random image from the matched items?

SELECT `item_images`.`filename` AS `url` FROM `item_images`
  INNER JOIN `item_link` ON `item_images`.`item_id` = `item_link`.`item_id`
  WHERE `item_link`.`category_id` = 1 
  GROUP BY `item_link`.`item_id`
  ORDER BY RAND()
  LIMIT 4
  • 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-13T19:20:19+00:00Added an answer on June 13, 2026 at 7:20 pm

    webduos is right distinct should solve your problem

    here is explaination

    SELECT distinct `item_images`.`filename` AS `url` FROM `item_images`
    INNER JOIN `item_link` ON `item_images`.`item_id` = `item_link`.`item_id`
    WHERE `item_link`.`category_id` = 1 
    GROUP BY `item_link`.`item_id`
    ORDER BY RAND()
    LIMIT 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MDB database which holds information about patients (customers). I am trying
I have a huge database which holds pairs of numbers (A,B), each ranging from
I have the following database tables, which hold information about a public transport system:
i have a database which contains an arabic tables , i can read it
I have a database which stores (among other things), the following pieces of information:
I am currently developing a Rhomobile application. I have a backend database which holds
I have a database table that holds the following grade information for a student:
I have a string that holds user input. This string can contain various types
I am building an application where a User can have many Clients (which cannot
I was given a database design which stores information about an organization and any

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.