[英]Php Mysql - Get value auto increment before and insert it to another column
[英]How to mirror auto increment value and combine with value in another column in mysql
我在表中有一個自動增量列id
,這很好用,但我正在嘗試創建一個order_id
列,它將用戶的account_id
與id
的自動增量值結合起來。
因此order_id
中的值看起來像這樣:
33.1000
52.1001
69.1002
Etc
其中點之前的數字是account_id
,之后的數字是id
的自動遞增值。
我怎樣才能做到這一點?
我發現的類似答案適用於另一種數據庫語言,或者是帶有死鏈接的問題。
我嘗試使用 PHP 插入后立即更新數據庫,最后插入的 id 為insert_id
但我確信這可以在 Z9778840A0100CB30C982876741B0 中完成。
您可以將值構造為:
concat(account_id, '.', lpad(id, 4, '0'))
不幸的是,您不能將其用作列的生成值或默認值。 這給了你兩個選擇。
首先是使用視圖:
create table v_t
select t.*, concat(account_id, '.', lpad(id, 4, '0')) as order_id
from t;
或者使用觸發器設置值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.