Randomly recommended 10 users can use ORDER by rand (), but a large amount of data will affect efficiency
$sql='select uid from member order by rand() limit 10';
Or a random uid, and then look from the UID, eg:
for($i=0;$i<10;$i++){$rand[]=mt_rand(1,100);}$sql="select uid from member where uid in (%s) and uid not in (select focus_uid from member_focus where uid ={$uid} ) limit 10";$sql = sprintf($sql,implode(',',$rand));
The problem is that the UID in the $rand may not be in the table member, so it is less than 10, Weibo is this? Is there any other plan?
Reply content:
Randomly recommended 10 users can use ORDER by rand (), but a large amount of data will affect efficiency
$sql='select uid from member order by rand() limit 10';
Or a random uid, and then look from the UID, eg:
for($i=0;$i<10;$i++){$rand[]=mt_rand(1,100);}$sql="select uid from member where uid in (%s) and uid not in (select focus_uid from member_focus where uid ={$uid} ) limit 10";$sql = sprintf($sql,implode(',',$rand));
The problem is that the UID in the $rand may not be in the table member, so it is less than 10, Weibo is this? Is there any other plan?
You can Gencun a random number to a word and index the field, and then generate a random number on the query to get a number of records greater than this random number.
Take Maxid and then randomly take 10 ID, query, judge whether attention, if missing, then randomly fetch 10, query, judge whether attention, and finally pieced together.
- Reads the total amount of the table in select COUNT (*) from TBL;
- The program gets the random 10 numbers that are not duplicated, the number range is 0~count (*)-1
- Execute the query in turn select * from TBL limit num,1;
- Remember to cache the data you've queried.