MySQL:通过创建的随机组递增ID

时间:2018-12-08 03:08:33

标签: php mysql phpmyadmin

我希望我的表为每个创建的组自动增加GroupId。这些组是根据userInformation表随机创建的。组信息表将随机组ID(从userInformation表中选择)存储到其表中。到目前为止,我已经能够增加它。我只得到零

$GroupSize = $_POST['groupsize'];

//Connect to the Server+Select DB
$con = mysqli_connect($host, $user, $password, $dbName) or die("Nope");

if (isset($_POST['create'])) {

//assign group id to groups created
//insert groupinformation to table from userInformation


//What I Have Tried
     //$query = "INSERT INTO groupInformation SELECT RegistrationId FROM (Select RegistrationId from userInformation order by RAND() LIMIT ".$GroupSize.")";
    //$query = "INSERT INTO groupInformation(RegistrationId) SELECT RegistrationId FROM (Select * from userInformation order by RAND() LIMIT ".$GroupSize.") AS j";




    $query = "INSERT INTO groupInformation(RandomGroupId,RegistrationId) SELECT randomRegistrationId,RegistrationId FROM (Select *,RAND() AS randomRegistrationId FROM userInformation ORDER BY randomRegistrationId LIMIT ".$GroupSize." ) AS j";
    $result = mysqli_query($con, $query) or die ("query failed " . mysqli_error($con));



}

mysqli_close($con);

这是我的桌子:

groupInformation

userInformation

这是我当前的输出:

output

我需要这些组具有随机的GroupId来递增,例如(1004 1003 1000)= 1(RandomGroupId),(1004、1002、1000)= 2(RandomGroupId),...

1 个答案:

答案 0 :(得分:0)

尝试这种方式。我希望它将对您有帮助:

$ letters ='abcefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; $ verificationText = substr(str_shuffle($ letters),0,10);