如何在使用存储过程时创建具有自动增量的 id 字段?

Posted

技术标签:

【中文标题】如何在使用存储过程时创建具有自动增量的 id 字段?【英文标题】:How to create id field with auto increasement while using stored procedure? 【发布时间】:2020-03-15 16:03:35 【问题描述】:

我有这个存储过程。如何选择一个变量作为可以在 sp 的每一行结果中 +1 值的字段?我需要它来制作一个虚拟 id 字段。请帮帮我。

CREATE DEFINER=`root`@`localhost` PROCEDURE `customer_order$count_order_by_month`()
BEGIN
    select count(1) as amount, month(created_date) as months, year(created_date) as years
    from customer_order
    group by   fn month(created_date) , MONTH(created_date), YEAR(created_date)
    order by Year(created_date), month(created_date);
END

【问题讨论】:

【参考方案1】:

你只是想要row_number()吗?

select row_number() over (order by min(created_date)) as seqnum,
       count(1) as amount, month(created_date) as months, year(created_date) as years
from customer_order
group by   fn month(created_date) , MONTH(created_date), YEAR(created_date)
order by Year(created_date), month(created_date);

【讨论】:

非常感谢。我不知道基本的 sql 查询。

以上是关于如何在使用存储过程时创建具有自动增量的 id 字段?的主要内容,如果未能解决你的问题,请参考以下文章