首页 > 解决方案 > 生成顺序参考编号

问题描述

插入时,我正在生成连续的参考编号,如投诉/1、投诉/2。我得到了先前生成的值(投诉/2),然后递增。

但是当两个用户同时提交时,我有时会得到两个投诉的相同参考号。

我该如何防止这种情况?

    SELECT RIGHT(Date_format(from_financial_year_,'%Y'),2), 
       RIGHT(Date_format(to_financial_year_,'%Y'),2) 
INTO   from_financial_year_, 
       to_financial_year_;SELECT   rec.receipt_ref_no 
INTO     last_receipt_ref_num_ 
FROM     svk_apt_receipts rec 
WHERE    Replace(Substring_index(rec.receipt_ref_no, '/', 2),'REC/','') = Concat(from_financial_year_,to_financial_year_)
AND      rec.customer_id = customer_id_ 
AND      rec.association_id = association_id_ 
ORDER BY rec.receipt_id DESC limit 1;IF(last_receipt_ref_num_ IS NULL) then 
SELECT 1 
INTO   max_ref_id_; 

else 
SELECT (replace(last_receipt_ref_num_, concat('REC/',from_financial_year_,to_financial_year_,'/'),'')+1)
INTO   max_ref_id_;ENDIF;SELECT Concat('REC/',from_financial_year_,to_financial_year_,'/',max_ref_id_)
INTO   receipt_ref_no_;INSERT INTO svk_apt_receipts 
    ( 
      receipt_ref_no, paid, payable, is_paid, master_receipt_to_id, receipt_from_id, receipt_to_id, receipt_date, 
      receipt_mode, transaction_ref_no, customer_id, association_id, is_active, created_by, created_on, receipt_status_id, remarks 
    ) 
    VALUES 
      ( receipt_ref_no_, _total_amount, 0,1,3, receipt_from_id_, receipt_to_id_, Cast(Now()AS DATE), 3, _transaction_ref_no, 
      customer_id_, association_id_, 1, _created_by, Now(), 2, 'Paid through Payment Gateway' 
    );

标签: phpmysqlsymfonylockingmariadb

解决方案


正如评论中提到的,只需存储一个自动递增的 id。所有其他的东西都可以通过琐碎的查询和/或您的表示层来处理。

举例来说...

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id SERIAL PRIMARY KEY
,user INT NOT NULL
);

INSERT INTO my_table (user) VALUES
(1),(1),(3),(3),(5),(2),(1),(8),(4),(5),(7),(5),(5),(4),(1),(2),(3),(6),(4),(6),(1),(5),(1),(8);

SELECT * FROM my_table;
+----+------+
| id | user |
+----+------+
|  1 |    1 |
|  2 |    1 |
|  3 |    3 |
|  4 |    3 |
|  5 |    5 |
|  6 |    2 |
|  7 |    1 |
|  8 |    8 |
|  9 |    4 |
| 10 |    5 |
| 11 |    7 |
| 12 |    5 |
| 13 |    5 |
| 14 |    4 |
| 15 |    1 |
| 16 |    2 |
| 17 |    3 |
| 18 |    6 |
| 19 |    4 |
| 20 |    6 |
| 21 |    1 |
| 22 |    5 |
| 23 |    1 |
| 24 |    8 |
+----+------+
24 rows in set (0.01 sec)

SELECT x.*
     , COUNT(*) complaint 
  FROM my_table x 
  JOIN my_table y 
    ON y.user = x.user 
   AND y.id <= x.id 
 GROUP 
    BY x.id 
 ORDER 
    BY user
     , id;
+----+------+-----------+
| id | user | complaint |
+----+------+-----------+
|  1 |    1 |         1 |
|  2 |    1 |         2 |
|  7 |    1 |         3 |
| 15 |    1 |         4 |
| 21 |    1 |         5 |
| 23 |    1 |         6 |
|  6 |    2 |         1 |
| 16 |    2 |         2 |
|  3 |    3 |         1 |
|  4 |    3 |         2 |
| 17 |    3 |         3 |
|  9 |    4 |         1 |
| 14 |    4 |         2 |
| 19 |    4 |         3 |
|  5 |    5 |         1 |
| 10 |    5 |         2 |
| 12 |    5 |         3 |
| 13 |    5 |         4 |
| 22 |    5 |         5 |
| 18 |    6 |         1 |
| 20 |    6 |         2 |
| 11 |    7 |         1 |
|  8 |    8 |         1 |
| 24 |    8 |         2 |
+----+------+-----------+
24 rows in set (0.00 sec)

推荐阅读