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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:42:06+00:00 2026-05-28T05:42:06+00:00

I have a table which includes all the users video downloading history: src_ip vlan_id

  • 0

I have a table which includes all the users video downloading history:

src_ip   vlan_id  video_id area  date_pl
aaaa     A        1        123   xxxx-xx-xx
aaaa     A        2        123   xxxx-xx-xx
aaaa     B        1        456   xxxx-xx-xx
bbbb     A        4        123   xxxx-xx-xx
bbbb     C        6        567   xxxx-xx-xx
...

I use the combination of src_ip and vlan_id to identify different users, Now I want to randomly select 50 different users (which means 50 distinct combinations of src_ip and vlan_id )and list all their downloading history.

So first I did:

   SELECT distinct src_ip, vlan_id from video_2 as table2 
   WHERE date_pl >= '2011-11-29 00:00' AND date_pl <= '2011-12-05 23:55' order by rand() limit 50

Then I did:

   SELECT src_ip, vlan_id, video_id, area from video_2 
   where video_2.src_ip = table2.src_ip AND video_2.vlan_id = table2.vlan_id 
   AND date_pl >= '2011-11-29 00:00' AND date_pl <= '2011-12-05 23:55'

But the problem is, since they are two independent queries, in the second query, it doesn’t know what table2 is.

How can I solve this problem or how to merge these two queries into one?

  • 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-28T05:42:06+00:00Added an answer on May 28, 2026 at 5:42 am
    CREATE TEMPORARY TABLE table2
    AS
    SELECT distinct src_ip, vlan_id from video_2
    WHERE date_pl >= '2011-11-29 00:00' AND date_pl <= '2011-12-05 23:55'
    ORDER BY rand() 
    LIMIT 50;
    
    SELECT src_ip, vlan_id, video_id, area
    FROM 
       video_2
       JOIN 
       table2 ON video_2.src_ip = table2.src_ip AND video_2.vlan_id = table2.vlan_id';
    

    You can can’t make “table2” derived in line because of restrictions on the use of LIMIT in subqueries. Otherwise it’d be

    SELECT 
       v2.src_ip, v2.vlan_id, v2.video_id, v2.area 
    FROM 
       video_2 v2
       JOIN 
       (SELECT distinct v.src_ip, v.vlan_id from video_2 v
       WHERE v.date_pl >= '2011-11-29 00:00' AND v.date_pl <= '2011-12-05 23:55'
       ORDER BY rand() 
       LIMIT 50
       ) table2 ON v2.src_ip = table2.src_ip AND v2.vlan_id = table2.vlan_id;
    

    Edit: Restrictions LIMITs in subqueries apply to IN mostly, not derived tables

    Edit 2: added more aliases

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

Sidebar

Related Questions

I have a table in the database which includes all active users. I then
I have a database table (named Topics) which includes these fields : topicId name
I have a table of names and addresses, which includes a postcode column. I
I have a mysql table which will store users email addresses (each is unique
I have a table which is full of arbitrarily formatted phone numbers, like this
I have a table which is referenced by foreign keys on many other tables.
I have a table which contains my server status create table ServerStatus ( ServerId
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have a table which holds flight schedule data. Every schedule have an effective_from
I have a table which has two varchar(Max) columns Column 1 Column 2 -----------------------

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.