Content Entry

1.6 在 WHERE 子句中引用取别名的列

Published: 2008-11-13 Categories: Cookbook Tags: SQL DB2 MySQL Oracle PostgreSQL Database MSSQL

Q: 在 where 子句中引用别名
A: 将查询作为内联视图就可以引用其中的别名

Oracle,MySQL,PostgreSQL,MSSQL,DB2 (使用内联视图)
SELECT *
FROM (
SELECT sal AS salary, comm AS commission
FROM emp
) x
WHERE salary < 5000 AND commission IS NOT NULL

+--------+------------+
| salary | commission |
+--------+------------+
| 1600 | 300 |
| 1250 | 500 |
| 1250 | 1400 |
| 1500 | 0 |
+--------+------------+
4 rows in set (0.00 sec)

PS: 子句优先级 FROM -> WHERE -> SELECT

Tables Used:
for MySQL
http://www.hooto.com/home/rui/doc/archives/5089.html
for PostgreSQL
http://www.hooto.com/home/rui/doc/archives/5090.html

Learn:
SQL Cookbook, by Anthony Molinaro.
Copyright 2006 O'Reilly Media, Inc.

--EOF--

comments loading