Skip to content

Instantly share code, notes, and snippets.

@consatan
Created November 30, 2015 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save consatan/1161170f796687f9ede5 to your computer and use it in GitHub Desktop.
Save consatan/1161170f796687f9ede5 to your computer and use it in GitHub Desktop.
通过子查询更新或插入固定前缀的递增字段
# 如果前缀要使用年份加月份,第7行需要增加判断条件
UPDATE `table` SET `no`=
(
SELECT `no`
FROM (
SELECT IF(
(SELECT 1 FROM `table` WHERE `no` IS NOT NULL AND `no` <> "") IS NULL,
"NO1000",
(
SELECT CONCAT("NO", SUBSTRING(`no`, 3) + 1)
FROM `table`
WHERE `no` IS NOT NULL AND `no` <> "" ORDER BY `no` DESC LIMIT 1
)
) AS `no`
) AS `t`
)
WHERE `uid`=?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment