有没有一种方法可以随机选择一个ID进入数据库

我正在填写汽车经销店预订表格,我需要随机选择一名工作人员进行试驾


我试过mt_rand()但我想我把它放错了位置


````````````````````````````````````

$sql = "INSERT INTO bookingcars (BookingStart, BookingEnd, BookingDate, MemberReference, ActivityID staffID) VALUES ('$timeStart', '$timeEnd', '$startDate', '$memberReference', '$activity', '$mt_rand(1, 4)staffID'";

````````````````````````````````````


波斯汪
浏览 174回答 2
2回答

天涯尽头无女友

虽然您可以在查询中构建它,但是如果执行以下操作,它的清洁度会更高,并且更容易调试:$staff=rand(1,4).'staffID'; // assume you wanted 1staffID .. ?//or did you just want $staff=rand(1,4);$sql = "INSERT INTO bookingcars (BookingStart, BookingEnd, BookingDate, MemberReference, ActivityID, staffID) VALUES ('$timeStart', '$timeEnd', '$startDate', '$memberReference', '$activity', '$staff')";

紫衣仙女

另一种方法是在您的查询值中添加一个子查询,例如 ,...) VALUES (..., (SELECT DISTINCT (Id) FROM stalesstafftable order by Rand() limit 1))
打开App,查看更多内容
随时随地看视频慕课网APP