php - 生成连续的引用号

标签 php mysql symfony locking mariadb

当插入时,我会生成连续的引用号,例如投诉/1、投诉/2。我得到了之前生成的值(complaint/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' 
    );

最佳答案

正如评论中提到的,只需存储一个自动递增的 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)

关于php - 生成连续的引用号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58028118/

相关文章:

PHP/MySQL 帖子类别

php - CodeIgniter、Active Record 还是查询生成器?

php - 当改变位置超过 1 时,Doctrine Extensions Sortable 无法正常工作

php - 如何在 PHP 中上传和插入多张图片?

php - 如何从 2 个 SQL 表中获取数据并将其放入单个 HTML 表中?

MySQL 对多个表进行排序需要很长时间

sql - 帮助编写一个 mysql 查询 - 这必须已经完成了 1000 次,但我正在努力..请帮助?

php - Doctrine setParameter 和无效参数号

symfony - Symfony 3 实体的自定义 JSON 序列化程序

php - 使用 JavaScript 进行 RPC(远程过程调用)