2012-11-19 58 views

回答

4

下面是一個例子:

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) 
+0

不錯'哪裏錯'。或者'另一種方言中的1 <> 1' – Davos

4

爲什麼你真的願意做,這是超越我:

CREATE VIEW foo AS SELECT NULL AS age, NULL AS id, NULL AS gender WHERE FALSE 
+1

也許這個問題應該是:我如何......以及爲什麼?這將是更有趣的嘗試和回答。 – Hogan

+0

哦,我敢打賭,我知道......測試案例。 – Hogan

+0

如果您希望稍後以左連接或完整連接方式使用此視圖,並且以編程方式希望確保視圖以正確的列存在,即使它並不總是需要有數據。例如如果你在這些列上使用'ISNULL' /'COALESCE' /'NVL'或者類似的函數。 – Davos

0

嘗試從這個SELECT語句定義視圖:

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

這會給你正確類型的列

相關問題