限制输出结果
SQL Serve
select top 5 prod_name
from Products;
DB2
select prod_name
from Products
fetch first 5 rows only;
Oracle
select prod_name
from Products
where rownum <= 5;
MySQL、MariaDB、PostgreSQL、SQLite
select prod_name
from Products
limit 5;
延申:输出从第5行开始的4行数据
select prod_name
from Products
limit 4 offset 5 ;
select prod_name
from Products
limit 5,4
拼接字段
SQL Serve
select vend_name + '(' + vend_country + ')'
from Vendors
order by vend_name;
select rtrim(vend_name) + '(' + rtrim(vend_country) + ')'
from Vendors
order by vend_name;
DB2、Oracle、PostgreSQL、SQLite
select vend_name || '(' || vend_country || ')'
from Vendors
order by vend_name;
select rtrim(vend_name) || '(' || rtrim(vend_country) || ')'
from Vendors
order by vend_name;
MySQL、MariaDB
select Concat(vend_name, ' (', vend_country, ')')
from Vendors
order by vend_name;
延申:
常用的文本处理函数
函数 | 说明 |
---|
left() 、right() | 返回字符串左边/右边的字符 | length() | 返回字符串的长度 | lower() 、upper() | 将字符串转换成小写/大写 | ltrim() 、rtrim() 、trim() | 去掉字符串左边/右边/两边的空格 | substring() | 提取字符串的组成部分 | soundex() | 返回字符串的soundex值(发音类似) |
日期和时间处理函数
SQL Serve
select order_num
from Orders
where datepart(yy,order_date) = 2020;
PostgreSQL
select order_num
from Orders
where date_part('year',order_date) = 2020;
Oracle
select order_num
from Orders
where extract(year from order_date) = 2020;
select order_num
from Orders
where order_date between to_date('2020-01-01','yyyy-mm-dd')
and to_date('2020-12-31','yyyy-mm-dd');
DB2、MySQL、MariaDB
select order_num
from Orders
where year(order_date) = 2020;
SQLite
select order_num
from Orders
where strftime('%Y',order_date) = '2020';
DBMS函数的差异汇总
函数 | 语法 |
---|
提取字符串的组成部分 | DB2、Oracle、PostgreSQL、SQLite 使用substr() MySQL、MariaDB、SQL Server 使用 substring() | 数据类型转换 | Oracle 使用多个函数,每个类型转换对应一个函数 DB2、PostgreSQL使用cast() MySQL、MariaDB、SQL Server 使用 convert() | 提取当前日期 | DB2、PostgreSQL使用current_date() MySQL、MariaDB使用curdate() Oracle使用sysdate SQL Server使用getdate() SQLite 使用date() |
未完待遇
|