SELECT
[DISTINCT|ALL]
select_list
FROM table_list
[where_clause]
[group_by_clause]
[HAVING condition]
[order_by_clause]
说明:
[DISTINCT|ALL]:是否去除列表中的重复记录
select_list:需要查询的字段列表
FROM:关键字,用于表示数据来源
{
*|
{
[ schema. ]{ table | view } .*
| expr [ [ AS ] c_alias ]
}
}
说明:
schema:模式名称
table | view:表或视图。
Expr:表达式。
C_alias:别名。
SELECT a1,b1.c1 FROM table_name;
SELECT * FROM table;
SELECT a1 AS 壹,a2 AS贰,a3 叁 FROM table_name;
AS关键字可省略,加空格即可。
SELECT a1,a2,a3 || ‘*’ || 100 || ‘=’ || a3*100 FROM table_name;
||代表连字符,类似JAVA中的 +
SELECT a1,a2,subStr(a3,1,6) AS 截取后的字符串 FROM table_name;
SELECT distinct(a1) FROM table_name;
ORDER BY
{expr | postion | c_alias}
[ASC | DESC]
[NULLS FIRST | NULLS LAST]
[ , {expr | position | c_alias}
[ASC | DESC]
[NULLS FIRST | NULLS LAST]
]...
说明:
ORDER BY:排序关键字
expr:表达式
position:表中列的位置
c_alias:别名
[NULLS FIRST | NULLS LAST]:空值处理方式
SELECT a1,a2,a3 FROM table_name ORDER BY a1 DESC;
最后加了DESC为降序,加ASC为升序,默认不加为升序
SELECT a1,a2,a3 FROM table_name ORDER BY a1 NULLS FIRST;
NULLS FIRST即为NULL值排在首位,NULLS LAST即为NULL值排在末尾。
SELECT a1,a2,a3 排序 FROM table_name ORDER BY 排序;
SELECT a1,a2,a3 FROM table_name ORDER BY a1*a2;
表达式中NULL和其他值相乘结果为NULL
SELECT a1,a2,a3 FROM table_name ORDER BY 3;
即意为按a3排序
SELECT a1,a2,a3 FROM table_name ORDER BY a1 ASC,3 DESC;
当第一个字段内值相同时会按第二个条件排序
GROUP BY
{ expr
| { ROLLUP | CUBE } {{expr [, expr ]...}}
}
说明:
Expr:通常为列名
ROLLUP | CUBE:可以返回小计和总计
b)GROUP BY不许出现在WHERE中,但是可以出现在WHERE后面,来处理WHERE过滤后的数据。
SELECT a1,a2,a3,AVG(a4) FROM table_name GROUP BY a1 HAVING AVG(a4)>1000
此处略去子查询的基本使用方式,仅说明量化比较关键字用法:
SELECT a1,a2,a3 FROM table_name WHERE a1< ANY(SELECT b1 FROM table_name)