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

查看:215
本文介绍了使用自动递增的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天全站免登陆