Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个包含两个整数列的表。一个自增索引号和编号。
我想要做的是插入一个新行并使用具有最高索引号的行的数值。
我想象这样的事情:
INSERT INTO `table`( `number`) VALUES (select `number` from `table` ORDER BY `index` DESC LIMIT 1)
我认为您发布的查询会起作用。这是另一种方法:
INSERT INTO `table`(`number`) SELECT MAX(`number`) FROM `table` WHERE `index` = (SELECT MAX(`index`) FROM `table`)
我不确定它是否更快,但您可以验证这一点。
例如,如果最大值是 999 并且多于一行的值是 999 ,SELECT MAX(number)则可以防止返回多个值。indexindex
SELECT MAX(number)
index