I am creating a program where data I select from one table is being randomly grouped. The RegistrationID number from each group is saved into another table (as a foreign key) and every group member is assigned a groupID that is auto incremented with every new group created.
$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
$query = "SELECT RegistrationId FROM (Select * from userInformation order by RAND() LIMIT ".$GroupSize.") INTO groupInformation";
$result = mysqli_query($con, $query) or die ("query failed" . mysqli_error($con));
//display group and information
echo "<table border='1' >";
echo "<tr><th>RegId</th><th>Name</th><th>Address</th><th>Email</th></tr>";
while (($row = mysqli_fetch_row($result)) == true) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>";
}
echo "</table>";
//if group is less than 2 create error message
}
mysqli_close($con);
My problem is that I cannot assigned the GroupId to the extracted results because auto incremented numbers cannot be copied. This is my error:
query failed Every derived table must have its own alias
Here are my Table schemas
Let GroupId
autoincrement,then create one more column RandomGroupId
in groupInformation
, then paste the query:
INSERT INTO groupInformation(RandomGroupId,RegistrationId)
SELECT randomRegistrationId,RegistrationId
FROM (Select *,RAND() AS randomRegistrationId
FROM userInformation ORDER BY randomRegistrationId LIMIT ".$GroupSize."
) AS j
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.