UPDATE1:您可以将您的包装REPLACE INTO
成一个存储过程
DELIMITER $$
CREATE PROCEDURE sp_replace(IN pid INT, IN chk INT, IN dt DATETIME)
BEGIN
IF 10 < COALESCE(TIMESTAMPDIFF(MINUTE,
(SELECT datetime FROM table_a WHERE p_id = pid), dt), 11) THEN
REPLACE INTO table_a (p_id, `check`, datetime) VALUES (pid, chk, dt);
END IF;
END$$
DELIMITER ;
这是SQLFiddle演示
假设它P_ID
有唯一的约束,你可以用INSERT ... SELECT ... ON DUPLICATE KEY UPDATE
它来做这样的工作
INSERT INTO table_a (p_id, `check`, datetime)
SELECT 5, 1, '2013-06-24 11:10:00'
FROM table_a
WHERE 10 < COALESCE(TIMESTAMPDIFF(MINUTE, '2013-06-24 11:00:00',
(SELECT datetime FROM table_a WHERE p_id = 5)), 11)
ON DUPLICATE KEY UPDATE `check` = VALUES(`check`), datetime = VALUES(datetime);
这是SQLFiddle演示
注意:两者REPLACE INTO
并INSERT INTO ON DUPLICATE KEY
更改自动递增的主键(在您的情况下为 f_id)