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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:36:09+00:00 2026-06-14T09:36:09+00:00

I have a nested subquery that selects a random AlbumID that the selected video

  • 0

I have a nested subquery that selects a random AlbumID that the selected video is in (videos can be in multiple albums), and the outer query then returns the videos and album information based on that AlbumID.

The problem is that the query is returning mixed results; sometimes it gives me some of the videos from one album, sometimes it gives videos from multiple albums, sometimes it returns nothing.

The outer query works if I specify a specific AlbumID instead of the subquery, and the subquery by itself correctly returns 1 random AlbumID. But put together, it’s giving me mixed results. What am I missing? Why is it returning varying amounts of rows, and multiple albums?

I’ve replicated the issue with test data, you can find the CREATE queries here: http://pastebin.com/raw.php?i=e6HaaSGK

The SELECT SQL:

SELECT  
    Videos_Demo.VideoID,
    VideosInAlbums_Demo.AlbumID
FROM 
    VideosInAlbums_Demo
    LEFT JOIN
        Videos_Demo
        ON Videos_Demo.VideoID = VideosInAlbums_Demo.VideoID
WHERE
    VideosInAlbums_Demo.AlbumID = (
                                    SELECT
                                        AlbumID
                                    FROM
                                        VideosInAlbums_Demo
                                    WHERE
                                        VideoID = '1'
                                    ORDER BY
                                        RAND()
                                    LIMIT 1
                                    )
  • 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-14T09:36:10+00:00Added an answer on June 14, 2026 at 9:36 am

    Try this. Moving the subquery to the JOIN seems to fix the problem. I think the problem has to do with having the subquery in the WHERE clause. I think that in the WHERE clause, the subquery and RAND function is being getting executed for each record. This is probably why the results are varying.

    SELECT  a.AlbumID,
            Videos_Demo.VideoID,
            VideosInAlbums_Demo.AlbumID
    
    FROM    VideosInAlbums_Demo
    
            LEFT JOIN Videos_Demo
            ON Videos_Demo.VideoID = VideosInAlbums_Demo.VideoID
    
            JOIN 
            (
                SELECT  AlbumID
                FROM    VideosInAlbums_Demo
                WHERE   VideoID = '1'
                ORDER BY RAND()
                LIMIT 1
            ) AS a ON VideosInAlbums_Demo.AlbumID = a.AlbumID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly complicated SQL query with a nested subquery. When I try
I have two nested packages that I am calling via fast_cgi. From the first
I have nested ul/li's and the problem is that if you add a background
I have a query that looks a bit like this (note: the actual query
I have to write a query to database, that has such table: TABLE HISTORY:
I have a fairly complex query in which I am working with with multiple
Is there a better way to perform this query without using a nested subquery?
When I have nested objects, the timing of events that occur when a mouse
I have nested ajax calls that are not working in ie7. They work fine
I have a complex nested-query which is inside a join, is it possible to

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.