我有以下SQL表:
bid btype Name world vi
---|----|------------ |--------|---------
1 | 1 | Business 1 | 0 | 44
2 | 4 | Business 2 | 0 | 55
5 | 5 | Business 3 | 0 | 23
3 | 1 | Business 4 | 1 | 99
4 | 2 | Business 5 | 0 | 12
6 | 3 | Business 6 | 0 | 14
7 | 2 | Business 7 | 1 | 55
8 | 1 | Business 8 | 2 | 66
9 | 2 | Business 9 | 2 | 77
10 | 1 | Business 10 | 3 | 88
我想要的是根据其“ btype”逐渐增加“ world”列中的值,例如,每一行都以“ world列”中的值0开头,因为这是“ btype”列中的首次此类值插入后,我要检查的是是否已经插入了“ btype”,以便“ world”列不再采用0而是1的值,依此类推...我想要实现的是,可以如果两个行共享相同的“ btype”和相同的“ world”,则“ btype”可以相同,但不能是“ world”,它必须不同,我希望它逐渐增加。
我将如何做这样的事情?
我通常不会做查询这个样子,所以我不能肯定地说,但一些像这样也许应该做的伎俩。
INSERT INTO theTable(btype, Name, world, vi)
SELECT [val1]
, [val2]
, IFNULL((SELECT MAX(world) FROM theTable WHERE btype = [val1]),-1)+1
, [val3]
;
您甚至可以只将第三个选择表达式包括在常规INSERT ... VALUES值列表中。但是就像我说的那样,我通常不会这样查询。(我属于明显的少数派,喜欢在插入之前先进行检查;但不能代替适当的唯一性约束。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句