Skip to content
Advertisement

How to create id field with auto increasement while using stored procedure?

I have this stored procedure. How can I select a variable as a field that can +1 value in every row of results of sp? I need it for making a virtual id field. please help me.

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

Advertisement

Answer

Do you simply want 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);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement