Mysql query

how to use mysql to get data as a percentage, for example, in a table, type=1 randomly takes 40% type=2, 30% type=3, 20% type=3, the rest 10%
language is thinkphp, how to write sql

May.09,2022

Please set a total number of records to get, for example: 100.

randomly take 40% of type = 1 data, that is, 100 * 40% = 40 items:

$total = 100;
$type_for_4 = floor($total * 0.4);
$id_list = db()->name('tb_name')->where('type' , 1)->column('id');
$id_list = random($id_list , $type_for_4);
$res = db()->name('tb_name')->where('type' , 1)->whereIn('id' , $id_list)->select();
//  type = 1 
print_r($res);
//  type =2 , 3 , 

1, get the total number of data of the specified type (count query);
2, calculate the total number of data to be fetched by percentage;
3, use limit to query the specified amount of data.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb86e8-484ac.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb86e8-484ac.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?