如何在数据库表中随机排序数据并将结果插入到其他表中? 我的意思是我在一个名为house的表中有一个行列表。然后我需要获取这些行并随机排序并插入到名为winner的其他表中。
我怎么能用php做到这一点?
//***Menaharia and Studio Applicants***//
$aplSTMen=mysql_query('select * from applicant where HouseType="Studio" AND SiteId="1"');
$ApConutTotMenST = mysql_num_rows($aplSTMen);
$querysStudiomenaharia=mysql_query('select * from house where HouseType="Studio" AND SiteId="1"');
$HoTotMenST = mysql_num_rows($querysStudiomenaharia);
//***THE NUMBER OF APPLICANTS ARE EQUAL TO THE NUMBER OF HOUSES***//
if( $HoTotMenST == $ApConutTotMenST) {
while($rw1=mysql_fetch_array($aplSTMen)) {
$ApplicantId= $rw1['ApplicantId'];
$SiteId= $rw1['SiteId'];
$ServiceType= $rw1['ServiceType'];
$HouseType= $rw1['HouseType'];
$PhysicalStatus= $rw1['PhysicalStatus'];
$Gender= $rw1['Gender'];
$MartialStatus=$rw1['MartialStatus'];
$sql = mysql_query("INSERT INTO `condominium`.`winner` (`WinnerId`)VALUES ('$ApplicantId');");
}
$ararr[]=array($aplSTMen);
while($rw1=mysql_fetch_array($querysStudiomenaharia)) {
$ress=$rw1['reserved'];
if( $ress== "no") {
$BlockNo=$rw1['BlockNo'];
$SiteId=$rw1['SiteId'];
$HouseNumber=$rw1['HouseNumber'];
$ararr[]=$HouseNumber;
echo $HouseNumber."<br\>";
/*$sql = mysql_query("INSERT INTO `condominium`.`winner` (`WinnerId`, `SiteId`, `HouseNumber`, `BlockNo`)
VALUES ('$ApplicantId', '$SiteId',$HouseNumber , '$BlockNo');");*/
}
}
}
我已经尝试过以上但是没有用。
答案 0 :(得分:1)
无需复制内存中的数据。如果你想要一个伪随机顺序,只需添加&#34; ORDER BY rand();&#34;到MySQL查询结束。