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?
You can
can’tmake “table2” derived in linebecause of restrictions on the use of LIMIT in subqueries. Otherwise it’d beEdit: Restrictions LIMITs in subqueries apply to IN mostly, not derived tables
Edit 2: added more aliases