mysql插入时的增量权重字段

mysql插入时的增量权重字段,mysql,Mysql,我有一个包含以下列的表 id parent_id title description weight 每一行都属于父级。每一行都有一个权重,它决定了它在父页面上的顺序。 每次添加新行时,我希望该新行的权重等于父id+1的最大权重 有没有办法在insert查询中执行此操作?试试这个,我不确定它是否能工作,因为我手头没有sql server。但我觉得我以前做过类似的事情 INSERT INTO table (id, parent_id, title, description, weight) VA

我有一个包含以下列的表

id
parent_id
title
description
weight
每一行都属于父级。每一行都有一个权重,它决定了它在父页面上的顺序。 每次添加新行时,我希望该新行的权重等于父id+1的最大权重


有没有办法在insert查询中执行此操作?

试试这个,我不确定它是否能工作,因为我手头没有sql server。但我觉得我以前做过类似的事情

INSERT INTO table (id, parent_id, title, description, weight) 
VALUES (2,1,'title', 'description', 1+(SELECT MAX(weight) FROM table))

当然,用您的表名替换table,用您需要的值替换table。

试试这个,我不确定它是否能工作,因为我手头没有sql server。但我觉得我以前做过类似的事情

INSERT INTO table (id, parent_id, title, description, weight) 
VALUES (2,1,'title', 'description', 1+(SELECT MAX(weight) FROM table))
当然,用您的表名替换table,用您需要的值替换table。

这应该有效

INSERT INTO myTable (id,parent_id,title,description,weight)
VALUES (idValue,parentValue,idValue,titleValue,descriptionValue,
        (SELECT MAX(weight) FROM myTable WHERE parent = parentValue))
这应该行得通

INSERT INTO myTable (id,parent_id,title,description,weight)
VALUES (idValue,parentValue,idValue,titleValue,descriptionValue,
        (SELECT MAX(weight) FROM myTable WHERE parent = parentValue))
如果 父项id=1和 id=111

如果 父项id=1和 id=111