首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能从另一个视图查询一个视图?

问不能从另一个视图查询一个视图?
EN

Database Administration用户
提问于 2013-03-14 17:52:31
回答 1查看 1.2K关注 0票数 2

好的,我正在将一个数据库从Server (我已经错过了)移动到MySql 5.6。我在创建一个有效日期列表时遇到了问题。在Server中,我只是使用CTE来建立数字,然后是月份,然后是数年,然后是一个月中的几天,然后交叉连接到一起创建一个有效的列表。我宁愿这样做,然后创建一个巨大的表,列出从开始到结束的所有有效日期。然而,将其转换为一组视图似乎不起作用:

代码语言:javascript
复制
create view `Numbers` as
    select 0 N union all select 1 union all select 2 union all
    select 3 union all select 4 union all select 5 union all
    select 6 union all select 7 union all select 8 union all select 9;
create view `Months` as
    select n.N+1 MonthNum union all select 11 union all select 12
    from `Numbers` n;

来自MySql的错误:错误代码: 1109。字段列表中的未知表n。

EN

回答 1

Database Administration用户

回答已采纳

发布于 2013-03-14 18:01:47

您必须将第二个视图重写如下:

代码语言:javascript
复制
create view `Months` as
    select N+1 MonthNum from `Numbers`
    union all select 11
    union all select 12
;

这是证据

代码语言:javascript
复制
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.10-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use test
Database changed
mysql> create view `Numbers` as
    ->     select 0 N union all select 1 union all select 2 union all
    ->     select 3 union all select 4 union all select 5 union all
    ->     select 6 union all select 7 union all select 8 union all select 9;
Query OK, 0 rows affected (0.05 sec)

mysql> create view `Months` as
    ->     select N+1 MonthNum from `Numbers`
    ->     union all select 11
    ->     union all select 12
    -> ;
Query OK, 0 rows affected (0.03 sec)

mysql>

查看查询的输出

代码语言:javascript
复制
mysql> select * from Months;
+----------+
| MonthNum |
+----------+
|        1 |
|        2 |
|        3 |
|        4 |
|        5 |
|        6 |
|        7 |
|        8 |
|        9 |
|       10 |
|       11 |
|       12 |
+----------+
12 rows in set (0.00 sec)

mysql>

试试看!!

票数 1
EN
页面原文内容由Database Administration提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://dba.stackexchange.com/questions/36714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档