mysql中大量新增一定規則的資料

來源:互聯網
上載者:User

標籤:mysql   大量新增   

在工作的過程中遇到問題

測試人員要求添加100個用於使用者名稱為wa000001 - wa000100 其他欄位保持不變

簡化表資料如下:

表名:t_user   ,user_id 為自增長主鍵

user_id   user_name

     7             wl

     9              ee

上面是原有表資料


要實現這個功能可以藉助系統資料表information_schema   實現如下:

定義變數 

 set @x = 0

select @x :[email protected] +1 seq from information_schema.COLUMNS limit 100

運行結果:

seq

1
2
3
4
5
6

.

.

100

將上面的sql語句和t_user表串連可得到想要的效果

運行之前必須將變數在設定為0
set @x = 0

INSERT t_user(user_name)SELECT
concat(‘wa000‘, lpad(a.seq, 3, ‘0‘))
FROM
(
SELECT
@x :[email protected] + 1 seq
FROM
information_schema. COLUMNS
LIMIT 100
)a

在查看t_user表

7 wl
9 ee
11 wa000001
13 wa000002
15 wa000003
17 wa000004
19 wa000005
21 wa000006
.

.

209 wa000100

該資料表主鍵自增長值為2



mysql中大量新增一定規則的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.