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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:29:49+00:00 2026-06-01T21:29:49+00:00

I have an incomplete query that I need some assistance with. It currently runs

  • 0

I have an incomplete query that I need some assistance with. It currently runs with a subselect which is inefficient and I am pretty sure using joins would make it a later faster.
Also, there is an element to the query that needs to be added that I’m not quite sure how to do. Details:

I have 3 tables

- album
 * albumId
 * albumName

- objectTag
 * objectTagObjectId (FK reference to albumId)
 * objectTabTagId (FK reference to tagId)

- tag
 * tagId
 * tagName

What I am trying to do is generate a list of random albums (let’s say 50) based on the tags linked to another album passed to the query.

For example, I have an album called “Britney Spears Greatest Hits” and that album is tagged with “pop” and “cheese”. I then pass the query the id of that Britney album and want the query to generate a list of 50 random other albums that are tagged with “pop” and “cheese”.

If there are only 20 albums with those tags then the query should populate the other 30 records with any random album irrespective of tag.

So far I can get the random albums based on tags:

SELECT albumId, albumName, objectTagTagId
FROM album
LEFT JOIN objectTag
  ON objectTagObjectId = albumId
WHERE objectTagTagId IN
(
  SELECT objectTagTagId
  FROM album
  LEFT JOIN objectTag
    ON objectTagObjectId = albumId
  WHERE albumId = 2471
)
ORDER BY RAND()
LIMIT 0,50

As mentioned however, this is inefficient and incomplete:

a) How do I make this more efficient with a join instead of a subselect? Is that possible?

b) How does this query need to be modified so that if there are only x amount of records that match the tags (let’s say 20) that the remainder are filled with random records irrespective of tag?

  • 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-01T21:29:51+00:00Added an answer on June 1, 2026 at 9:29 pm

    i cannot vouch for efficiency but here’s an idea…

    SELECT * FROM (
      SELECT albumId, albumName, objectTagTagId FROM (
        (
        SELECT count(*) AS cnt, Out.albumId, Out.albumName, objectTagTagId
        FROM album Src
        JOIN objectTag sT
          ON (Src.ablumId = sT.objectTagObjectId)
        JOIN objectTag oT
          USING (objectTabTagId)
        JOIN album Out
        ON (Out.albumId = oT.objectTagObjectId)
        WHERE Src.albumId = 2471
            AND Out.albumId != 2471
        GROUP BY albumId, albumName, objectTagTagId
        )
        UNION     
        (
        SELECT 0 AS cnt, albumId, albumName, null AS objectTagTagId
        FROM album
        ORDER BY RAND()
        LIMIT 50
        )
    
      ) foo ORDER BY cnt DESC LIMIT 50
    ) goo ORDER BY RAND()
    

    the count(*) / group by will count the number of tags an album has in common with the src album.

    The union will give us 50 filler albums with a cnt of zero, which is ORDERED to the bottom of foo by the ORDER BY and LIMIT.

    the ORDER BY RAND() on goo will randomize the order

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

Sidebar

Related Questions

I have created a SQL query that runs on SQL Server 2008. For some
I have a query result set that contains data that I need to filter,
I have a chain of asynchronous events that i want to execute, some of
I have a incomplete csv file that needs to be accurately updated, so there
I have a blocking function that executes an asynchronous MySQL query and returns the
I have been tasked with making some edits to a legacy system that is
I have a query in MySQL that returns three columns, a Candidate's ID, the
Let's say you have a SQL query that is returning n results and you
I have the following Mysql Table Order table status 0 - incomplete status 1
have written this little class, which generates a UUID every time an object of

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.