标题:
SQL CREATE VIEW 示例
[打印本页]
作者:
vicky.yu
时间:
2024-9-4 14:35
标题:
SQL CREATE VIEW 示例
示例一
考虑 CUSTOMERS 表,表中的记录如下所示:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
下面是由 CUSTOMERS 表创建视图的例子。该视图包含来自 CUSTOMERS 表的顾客的名字(name)和年龄(age):
SQL > CREATE VIEW CUSTOMERS_VIEW AS
SELECT name, age
FROM CUSTOMERS;
现在,你就可以像查询普通的数据表一样查询 CUSTOMERS_VIEW 了:
SQL > SELECT * FROM CUSTOMERS_VIEW;
上述语句将会产生如下运行结果:
+----------+-----+
| name | age |
+----------+-----+
| Ramesh | 32 |
| Khilan | 25 |
| kaushik | 23 |
| Chaitali | 25 |
| Hardik | 27 |
| Komal | 22 |
| Muffy | 24 |
+----------+-----+
示例2
下面是由 CUSTOMERS 表创建视图的例子。该视图包含来自 CUSTOMERS 表中年龄(age)为25的顾客的ADDRESS信息:
SQL > CREATE VIEW CUSTOMERS_ADDRESS AS
SELECT ADDRESS
FROM CUSTOMERS;
WHERE AGE=25;
我们可以像这样查询上面这个 CUSTOMERS_ADDRESS 视图:
SQL > SELECT * FROM CUSTOMERS_ADDRESS;
我们也可以向查询添加条件。现在,我们仅仅需要查看 "Delhi" 的数据:
SELECT * FROM CUSTOMERS_ADDRESS
WHERE ADDRESS='Delhi';
欢迎光临 赛捷软件论坛 (http://sagesoft.cn/bbs/)
Powered by Discuz! 7.2