在mysql中选择更新的记录

时间:2016-08-21 06:14:40

标签: mysql

如何在mysql中选择更新记录的ID;

有没有办法做这样的事情

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class CustomErrorController extends AbstractErrorController {
    public static final String ERROR_500 = "/500";
    private static final String ERROR_PATH=  "/error";

    @Autowired
    public CustomErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }


    /**
     * Responsible for handling all errors and throw especial exceptions
     * for some HTTP status codes. Otherwise, it will return a map that
     * ultimately will be converted to a json error.
     */
    @RequestMapping({ERROR_PATH,ERROR_500})
    public ResponseEntity<?> handleErrors(HttpServletRequest request) {
        return ResponseEntity.status(getStatus(request)).body(getErrorAttributes(request, false));
    }

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }
}

1 个答案:

答案 0 :(得分:0)

你不能,你必须在更新之前选择它们。

如果您的表使用InnoDB存储引擎,请使用SELECT FOR UPDATE来锁定所选行并阻止其他客户端插入,更新或删除所选行。

START TRANSACTION;
SELECT id FROM tblA WHERE col2 > 5 FOR UPDATE;
UPDATE tblA SET col1 = false WHERE col2 > 5;
COMMIT;

另一种方法是将某些列设置为客户端唯一的值(例如进程ID)以及使用该值进行选择时。 但它需要一个额外的列。

UPDATE tblA SET col1 = false, process_id = 12345 WHERE col2 > 5 AND process_id IS NULL;
SELECT id FROM tblA WHERE process_id = 12345