3

我正在尝试使用我定义的列在 mySQL 中创建一个空视图。

即我想要一个包含列的视图:年龄、id、性别,但有 0 行。

提前致谢。

4

3 回答 3

7

这是给你的一个例子:

mysql> create or replace view v_test as 
    -> select 99 as age, 
    ->   2147483647 as id, 
    ->   'F' as gender 
    -> from dual 
    -> where false;
Query OK, 0 rows affected (0.04 sec)

mysql> desc v_test;
+--------+------------+------+-----+---------+-------+
| Field  | Type       | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| age    | int(2)     | NO   |     | 0       |       |
| id     | bigint(10) | NO   |     | 0       |       |
| gender | varchar(1) | NO   |     |         |       |
+--------+------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> select * from v_test;
Empty set (0.00 sec)
于 2012-11-19T20:37:09.353 回答
7

为什么你会想要这样做超出了我的范围:

CREATE VIEW foo AS SELECT NULL AS age, NULL AS id, NULL AS gender WHERE FALSE
于 2012-11-19T19:49:58.973 回答
1

尝试从此选择语句定义您的视图:

SELECT 5 as age, 10 as id, 'M' as gender
WHERE age = 0

这将为您提供正确的列类型

于 2012-11-19T19:50:48.267 回答