How to echo random rows from database?

前端 未结 5 746
Happy的楠姐
Happy的楠姐 2020-12-06 22:26

I have a database table with about 160 million rows in it.

The table has two columns: id and listing.

I simply need to used PHP to

5条回答
  •  臣服心动
    2020-12-06 22:49

    You want to use the rand function in php. The signature is

    rand(min, max);
    

    so, get the number of rows in your table to a $var and set that as your max. A way to do this with SQL is

    SELECT COUNT(*) FROM table_name;
    

    then simply run a loop to generate 1000 rands with the above function and use them to get specific rows.

    If the IDs are not sequential but if they are close, you can simply test each rand ID to see if there is a hit. If they are far apart, you could pull the entire ID space into php and then randomly sample from that distribution via something like

    $random = rand(0, count($rows)-1);
    

    for an array of IDs in $rows.

提交回复
热议问题