with 子句
提供定义临时关系的方法,这个定义只对包含with子句的查询有效。with子句是在SQL:1999中引入的,目前许多(但并非所有)数据库系统都提供支持
with student_temp(id,name,age) as(
select id,name,age from student where age > 20
)
select * from student_temp;
如上,建立了一个student_temp 的临时表,后者再根据student_temp来查询
case 结构
case语句可以用在任务应该出现值的地方
select id ,
case
when address is null then '未知'
else address
end
from student;
如上,在查询student表时,如果address为null.那么就用 '未知' 代替。
create table 扩展
当需要创建表参考现有的表,或将查询出来的数据保存到新表中
创建一张表参考现有的表
create table tmep_usr like s_user;
将查询出来的数据保存到新表中
create table temp_user as (
select * from s_user where dept = 1
)with data;
上面的语法在不同的数据库系统中的实现也不一样,要参考相应的手册。