t_girl=# update t1 set log_time = now() where id in (1,2,3) returning *;
id | log_time
----+----------------------------
1 | 2014-11-26 11:06:53.555217
2 | 2014-11-26 11:06:53.555217
3 | 2014-11-26 11:06:53.555217
(3 rows)
UPDATE 3
Time: 6.991 ms
返回删除掉的行:
t_girl=# delete from t1 where id < 2 returning *;
id | log_time
----+----------------------------
1 | 2014-11-26 11:06:53.555217
(1 row)
DELETE 1
Time: 6.042 ms
返回插入后的行:
t_girl=# insert into t1 select 1,now() returning *;
id | log_time
----+----------------------------
1 | 2014-11-26 11:07:40.431766
(1 row)
INSERT 0 1
Time: 6.107 ms
t_girl=#
那在MySQL里如何实现呢?
我可以创建几张内存表来来保存这些返回值,如下:
CREATE TABLE t1_insert ENGINE MEMORY SELECT * FROM t1 WHERE FALSE;
CREATE TABLE t1_update ENGINE MEMORY SELECT * FROM t1 WHERE FALSE;
CREATE TABLE t1_delete ENGINE MEMORY SELECT * FROM t1 WHERE FALSE;
ALTER TABLE t1_insert ADD PRIMARY KEY (id);
ALTER TABLE t1_update ADD PRIMARY KEY (id);
ALTER TABLE t1_delete ADD PRIMARY KEY (id);
DELIMITER $$
USE `t_girl`$$
DROP TRIGGER /*!50032 IF EXISTS */ `tr_t1_insert_after`$$
CREATE
/*!50017 DEFINER = 'root'@'localhost' */
TRIGGER `tr_t1_insert_after` AFTER INSERT ON `t1`
FOR EACH ROW BEGIN
REPLACE INTO t1_insert VALUES (new.id,new.log_time);
END;
$$
DELIMITER ;
DELIMITER $$
USE `t_girl`$$
DROP TRIGGER /*!50032 IF EXISTS */ `tr_t1_update_after`$$
CREATE
/*!50017 DEFINER = 'root'@'localhost' */
TRIGGER `tr_t1_update_after` AFTER UPDATE ON `t1`
FOR EACH ROW BEGIN
REPLACE INTO t1_update VALUES (new.id,new.log_time);
END;
$$
DELIMITER ;
DELIMITER $$
USE `t_girl`$$
DROP TRIGGER /*!50032 IF EXISTS */ `tr_t1_delete_after`$$
CREATE
/*!50017 DEFINER = 'root'@'localhost' */
TRIGGER `tr_t1_delete_after` AFTER DELETE ON `t1`
FOR EACH ROW BEGIN
REPLACE INTO t1_delete VALUES (old.id,old.log_time);;
END;
$$
DELIMITER ;