我希望在范围内的某些列(唯一)值增加1,然后运行它;
java -cp .:filepath/mysql-connector-java-5.1.35-bin.jar App
但它会引发重复输入错误。但如果查询完全运行,那么不会重复
update policy set ORDERNUMBER = ORDERNUMBER +1 where ORDERNUMBER between 3 and 5
查询后我想要这个:
ORDERNUMBER
1
2
3
4
5
答案 0 :(得分:1)
尝试切换5和3,使其显示为:
update policy set ORDERNUMBER = ORDERNUMBER +1 where ORDERNUMBER between 3 and 5
更新: ORDERNUMBER似乎有一个独特的约束。你不能简单地添加一个,因为它将具有与另一行相同的值。使用这样的查询无法轻松完成此操作。
答案 1 :(得分:1)
正如Pete所说,使用uniqe constaint列无法做到这一点,所以删除或禁用它而不是自己创建;在我的场景中,我这样处理:
try {
$dbObject = Connection::getConnection();
$request = $dbObject->dbh->prepare($sqlForOrderNum);
$request->execute();
$currentorder = $request->fetchColumn();
if ($currentorder >= $policyObject->getOrdernumber())
$sql = "update policy set ORDERNUMBER = ORDERNUMBER +1 where ORDERNUMBER BETWEEN " . $policyObject->getOrdernumber() . " and $currentorder-1" . $sql;
else
$sql = "update policy set ORDERNUMBER = ORDERNUMBER -1 where ORDERNUMBER BETWEEN $currentorder+1 and " . $policyObject->getOrdernumber() . $sql;
$request = $dbObject->dbh->prepare($sql);
$request->bindParam(':id', $policyid, PDO::PARAM_INT);
$request->bindParam(':policyname', $policyObject->getPolicyName(), PDO::PARAM_STR);
$request->bindParam(':remark', $policyObject->getRemark(), PDO::PARAM_STR);
$request->bindParam(':access', $policyObject->getAccess(), PDO::PARAM_STR);
$request->bindParam(':enablelog', $policyObject->getEnableLog(), PDO::PARAM_STR);