【问题标题】:MySQL Increment Insert and Update ColumnMySQL 增量插入和更新列
【发布时间】:2018-02-16 03:57:05
【问题描述】:

我有一个包含以下表格字段的数据库:

id   no letter content
1    1         book
2    2         pen
3    3  a      apple
4    3  b      mango
5    3  c      banana
6    4         car

我将使用id=7, no=2, letter=null and content=moon 插入新值。 我的问题是mysql如何插入和更新命令 "no" 列所以查询结果将变为:?

no letter content
1         book
2         moon
3         pen
4  a      apple
4  b      mango
4  c      banana
5         car

【问题讨论】:

  • 让我们看看你正在使用的 SQL 字符串。

标签: java mysql jsp


【解决方案1】:
update table 
set no = no + 1 
where no >= 2
and id != 7

【讨论】:

  • 是的,您必须先更新。如果先插入,将有 2 行 no=2。如果您可以区分 2 行,您也可以在插入后更新 - 只需确保更新 where 以排除新插入的行。例如添加“and id != 7”(更新了答案以反映这一点)。
猜你喜欢
  • 2014-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多