使用自动递增的 id 列创建 MySQL 视图

2021-12-29 00:00:00 mysql view

我有一个用于创建视图的 MySQL 数据库.是否可以为视图中的每一行添加一个自动递增的 id?

I have a MySQL database from which a view is created. Is is possible to add an auto-incrementing id for each row in the view?

我试过了

CREATE ALGORITHM=UNDEFINED DEFINER=`database_name`@`%` SQL SECURITY DEFINER VIEW `MyView` AS 
set @i = 0;
select  @i:=@i+1 as `id`
        ...

但这在视图中不起作用.

but that doesn't work in a View.

推荐答案

抱歉 - 您不能在 VIEW 中自动递增(尽管您可以在存储过程中执行此操作).

Sorry - you can't autoincrement in a VIEW (You could do this in a Stored Procedure though).

来自MySQL手册:

视图定义受以下限制: SELECT语句不能引用系统或用户变量.

A view definition is subject to the following restrictions: The SELECT statement cannot refer to system or user variables.

相关文章