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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:31:14+00:00 2026-06-03T16:31:14+00:00

here is my 2 tables, id for a inner join event. i want to

  • 0

here is my 2 tables, id for a inner join event. i want to do this thing:

in table b, there has 10 albums, i want random get out 4 albums. then each album select one record, the record is random position in the album.

so that i will get 4 records back (these 4 records with no duplicate id), then take these 4 records for a inner join query, to get the title from table a.

here just little records just for test. in the fact, i have 300,000 records in table a and 2,000,000 records in table b.

table a

+-----+-------+
| id  | title | 
+-----+-------+
| 1   | a1    |
+-----+-------+
| 2   | a2    |
+-----+-------+
| 3   | a3    |
+-----+-------+
| 4   | a4    |
+-----+-------+
| 5   | a5    |
+-----+-------+
| 6   | a6    |
+-----+-------+

table b

+-----+--------+
| id  | album  | 
+-----+--------+
|  1  | album1 | 
+-----+--------+
|  2  | album1 | 
+-----+--------+
|  3  | album1 | 
+-----+--------+
|  6  | album1 | 
+-----+--------+
|  2  | album2 | 
+-----+--------+
|  3  | album2 | 
+-----+--------+
|  5  | album3 | 
+-----+--------+
|  6  | album3 | 
+-----+--------+
|  3  | album4 | 
+-----+--------+
|  2  | album5 | 
+-----+--------+
|  4  | album5 | 
+-----+--------+
|  5  | album5 | 
+-----+--------+
|  1  | album6 | 
+-----+--------+
|  3  | album6 | 
+-----+--------+
|  2  | album7 | 
+-----+--------+
|  4  | album7 | 
+-----+--------+
|  1  | album8 | 
+-----+--------+
|  5  | album8 | 
+-----+--------+
|  3  | album9 | 
+-----+--------+
|  2  | album10| 
+-----+--------+
|  5  | album10| 
+-----+--------+

I am not good at mysql query. In my mind I would do

select * from b group by album order by random() limit 0,4 

get back 4 album, then do a inner join query (this query not correct, how to check the b.id no duplicate?)

select * from b inner join a on b.id = a.id where (select id from b where b.album = '".$row['album']."'  order by random() limit 1) 

I need an easy and quicker method, the best is just use one query. many thanks.

  • 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-03T16:31:15+00:00Added an answer on June 3, 2026 at 4:31 pm

    AFAIR, “ORDER BY RAND()” is extremely slow solutions, especially on tables like you have (2 million+ records), so I’d recommend looking at something similar to these kind of articles first: http://www.greggdev.com/web/articles.php?id=6

    So, you should know the number of records in your table before running the query and then do something like:

    "SELECT * FROM `album` LIMIT 1 OFFSET " . rand(0,$count)
    

    This will return you 1 random row a bit more efficiently, I believe.

    Also, I think it’s not a good idea to store album references as string in tracks table, you’d rather use a proper integer foreign key album_id referenced to albums.id. Then you can join both tables much fatser. If I were you, I’d do first:

    ALTER TABLE `tracks` add column `album_id` int;
    UPDATE `tracks` SET `album_id` = SUBSTRING(`album`,5);
    

    Then, after doing this and combining with the solution above, launch something like:

    "SELECT * FROM `album` INNER JOIN `tracks`ON `tracks`.`album_id` = `albums`.`id` LIMIT 1 OFFSET " . rand(0,$count)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please look at this image here is 3 tables , and out i want
Here is my query select * from table1 inner join table2 on table1.typeId=table2.typeId; This
here is my 3 tables: table 1 -- stores user information and it has
I have a question. I have here 2 tables table 1 : products product_id
Here is the SQL SELECT tal.TrustAccountValue FROM TrustAccountLog AS tal INNER JOIN TrustAccount ta
Here's a query: SELECT *, COUNT(*) as `numauth` FROM `favorites` as `f1` INNER JOIN
Here is the script to create my tables: CREATE TABLE clients ( client_i INT(11),
I need to join a table on itself while joining other tables and doing
Trying to get an INNER JOIN to work is driving me insane, so I'm
I'm trying to write an INNER JOIN query. I wanted to get rows/information that

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.