gpt4 book ai didi

mysql - 基于 ID 的计算列,不生成收据

转载 作者:行者123 更新时间:2023-11-29 21:06:07 26 4
gpt4 key购买 nike

我在 SQL 中有下表:“收据编号”列是根据 PB + 年月日期计算的,其中有 9 个零加上第一列的值。

请帮我翻译以下 MYSQL 查询。

Create table PBTran
(
PBTranID int primary key auto_increment,
ReceiptNo VARCHAR(30) AS 'PB' + cast(datepart(yy,getdate()) as varchar(25)) + cast(datepart(mm,getdate()) as varchar(25)) + RIGHT('000000000' + CAST(PBTranID AS VARCHAR(10)), 9)
)

最佳答案

您可以使用 View :

Create table t_PBTran (
PBTranID int primary key auto_increment
);

Create view v_PBTran as
select PBTranId,
concat(date_format(now(), '%Y%m%d), lpad(PBTranId, 8, ' '))
from t_PBTran;

注意:我认为这并不能真正达到您想要的效果。 SQL Server 中的计算列将当前日期放入收据中。因此,该值每天都会发生变化。该 View 执行完全相同的操作。

如果您确实想要插入 ID 的日期,您有两个选择:

  1. 在表中有一列包含插入日期,并使用该列代替当前日期/时间。
  2. 使用触发器创建收据。

关于mysql - 基于 ID 的计算列,不生成收据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36813138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com