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

时间:2019-05-05 21:16:00

标签: php

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

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

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

2 个答案:

答案 0 :(得分:0)

虽然您可以在查询中构建它,但是如果执行以下操作,它会更干净,更易于调试:

$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')";

答案 1 :(得分:0)

另一种方式是在您的查询值中添加一个,...) VALUES (..., (SELECT DISTINCT (Id) FROM stalesstafftable order by Rand() limit 1))

之类的子查询。