我想再增加一列,根据另一列值自动增量:
Connected successfully to >> 127.0.0.1
array(2) { [0]=> string(18) "information_schema" ["Database"]=> string(18) "information_schema" } array(2) { [0]=> string(2) "db" ["Database"]=> string(2) "db" } array(2) { [0]=> string(5) "mysql" ["Database"]=> string(5) "mysql" } array(2) { [0]=> string(10) "phpmyadmin" ["Database"]=> string(10) "phpmyadmin" } array(2) { [0]=> string(4) "test" ["Database"]=> string(4) "test" }
这可能吗?
答案 0 :(得分:0)
built-in autoincrementing algorithm不能像这样工作。
要计算下一个IDForUser
值,请搜索具有相同UserID
(或零)的最大现有值,然后添加一个值。
如果您不想在代码中执行此操作,则必须使用trigger:
CREATE TRIGGER xxx
AFTER INSERT ON MyTable
FOR EACH ROW
WHEN NEW.IDForUser IS NULL
BEGIN
UPDATE MyTable
SET IDForUser = IFNULL((SELECT MAX(IDForUser)
FROM MyTable
WHERE UserID = NEW.UserID), 0) + 1
WHERE ID = NEW.ID;
END;