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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:27:44+00:00 2026-05-22T23:27:44+00:00

I have two tables The first with only 5 rows The second with 800

  • 0

I have two tables

The first with only 5 rows

The second with 800 rows

I’m using this query:

SELECT * 
FROM table1 t1 
JOIN (SELECT * FROM table2 ORDER BY RAND() LIMIT 5) t2 

But I’m getting 5 rows from the first table for each result of the second table.

I don’t need a condition when joining, I just want 5 random results from the second table to join the 5 results from the first.

Example:

--------------------------------------------------------
|table1 (always with same order)| table2(random order) |
--------------------------------------------------------
    item1                       |       item4
    item2                       |       item2 
    item3                       |       item5 
    item4                       |       item1
    item5                       |       item3
  • 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-22T23:27:45+00:00Added an answer on May 22, 2026 at 11:27 pm

    Do you mean UNION ?

    SELECT * FROM table1
    UNION SELECT * FROM table2 ORDER BY RAND() LIMIT 5;
    

    Update: revised answer after modification of your question:

    SELECT field1 FROM table1
    UNION SELECT field2 FROM table2 ORDER BY RAND() LIMIT 5;
    

    To my understanding, you just need one field from each table. If you need several ones, you can list them: field2, field2, … as long as the number of fields is the same in both SELECTs.


    Update 2: ok, I think I see what you mean now. Here is a (dirty) way to do it, I’m quite confident someone can come with a more elegant solution though:

    SET @num1=0, @num2=0;
    
    SELECT t1.field1, t2.field2
    FROM (
        SELECT field1, @num1:=@num1+1 AS num
        FROM table1
    ) AS t1
    INNER JOIN (
        SELECT field2, @num2:=@num2+1 AS num
        FROM (
            SELECT field2
            FROM table2
            ORDER BY RAND()
            LIMIT 5
        ) AS t
    ) AS t2
    ON t1.num = t2.num;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables which I want to join together using a left outer
I have a view that is joining two tables and ordering by the first
I have two tables, Book and Tag , and books are tagged using the
I have two instances of the same database. The first db represents data from
I have two tables that are joined together. A has many B Normally you
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType
I have two tables, both with start time and end time fields. I need
I have two tables containing Tasks and Notes, and want to retrieve a list
I have two tables in my database, called ratings and movies . Ratings: |
I have two tables, one that contains volunteers, and one that contains venues. Volunteers

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.