使用自动递增的 id 列创建 MySQL 视图 [英] Creating a MySQL view with an auto-incrementing id column

查看:56
本文介绍了使用自动递增的 id 列创建 MySQL 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于创建视图的 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.

这篇关于使用自动递增的 id 列创建 MySQL 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆