here are my 2 tables, id for internal join event. I want to do this:
in table b, there are 10 albums
, I want 4 albums
random output. then each album
select one record
, the record will be a random position in the album.
so that I get 4 records back (these 4 records with no duplicate id)
, then take these 4 records for an internal connection request to get the title
from table a
.
here are just small entries just for the test. in that I have 300,000 entries in table a and 2,000,000 entries 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 opinion, I would do
select * from b group by album order by random() limit 0,4
get back 4 albums, then execute an internal connection request (this request is not correct, how to check 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 a simple and quick method, it is best to use a single request. many thanks.