2022年数据库sql语句大全参照 .pdf
《2022年数据库sql语句大全参照 .pdf》由会员分享,可在线阅读,更多相关《2022年数据库sql语句大全参照 .pdf(29页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、数据库 sql 语句大全一、连接列值db2/oracle/postgresqlselect name(字段)|Works AS a(文字)|job as msg from emp where deptno=10;mysqlselect concat(name,works as a,job)as msg from emp where deptno=10;sql server select name+works as a+job as msg from emp where deptno=10;二、使用条件逻辑select name,salary,case when salary=4000 then
2、 over else ok end as status from emp 二、限制返回的行数db2select*from emp fetch first 5 rows only mysql/postgresqlselect*from emp limit 5 oracleselect*from emp WHERE rownum=5 名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 29 页 -sql serverselect top 5*from emp 三、随机返回记录db2select name,job from emp order by rand()fetch first 5
3、rows only mysqlselect name,job from emp order by rand()limit 5 postgresqlselect*from emp order by random()limit 5 oracleselect*from(select name,job from emp order by dbms_random.value()where rownum=5 sql serverselect top 5 name,job from emp order by newid()五、将空值转换成实际值select coalesce(comm,0)from emp
4、不为空则返回 comm 值,空则返回 0,comm 类型与 0 类型必须一致六、按子串排序(取消后面 2 位)db2/mysql/oracle/postgresqlselect name,job from emp order by substr(job,length(job)-2)sql serverselect name,job from emp order by substring(job,len(job)-2,2)七、对字母数字混合的数据排序名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 29 页 -oracle/postgresql select data from em
5、p v order by replace(data,replace(translate(data,0123456789,#),#,),)select data from emp order by replace(translate(data,0123456789,#),#,)db2 select*from(select ename|cast(deptno as char(2)as data from emp)v order by replace(data,replace(translate(data,#,0123456789),#,),)select*from(select name|cast
6、(deptno as char(2)as data from emp v order by replace(translate(data,#,0123456789),#,)mysql/sqlserver当前不支持 translate函数,无解决方案八、处理排序空值db2/mysql/postgresql/sqlserverselect name,sal,comm from(select name,sal,comm case when comm is null then 0 else 1 end as is_null from emp)x order by is_null desc,comm o
7、racleselect name,sal,comm from emp order by comm nulls last/all nulls last select name,sal,comm from emp order by comm nulls first/all nulls first 九、根据数据项的键排序名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 29 页 -select name,sal,job,comm from emp order by case when job=salesman then comm else sal end 十、记录集的叠加/使用 unio
8、n 子句相当于对使用union all子句的结果使用 distinct select ename as ename_and_dname,deptno from emp where deptno=10 union all select-,null from t1 union all select dname,deptno from dept 十一、从一个表红查找另一个表没有的值db2/postgresqlselect deptno from dept except select deptno from emp oracleselect deptno from dept minus select
9、deptno from emp mysql/sqlserver select deptno from dept where deptno not in(select deptno from emp)十二、在一个表中查找与其他表不匹配的记录db2/mysql/postgresql/sqlserver select d.*from dept d left outer join emp e on(d.deptno=e.deptno)where e.deptno is null oracle select d.*from dept d,emp e where d.deptno=e.deptno(+)a
10、nd e.deptno is null 十三、向查询中增加联接而不影响其他联接db2/mysql/postgresql/sqlserverselect e.ename,d.loc,eb.received from emp e join dept d on 名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 29 页 -(e.deptno=d.deptno)left join emp_bonus eb on(e.empno=eb.empno)order by 2 oracleselect e.ename,d.loc,eb.received from emp e,dept d,emp_b
11、onus eb where e.deptno=d.deptno and e.empno=eb.empno(+)order by 2 select e.ename,d.loc,(select eb.received from emp_bonus eb where eb.empno=e.empno)as received from emp e,dept d where e.deptno=d.deptno order by 2 十四、检测两个表中是否有相同的数据解决原理:1、首先,查找处表emp中存在而视图 v 中没有的行2、然后合并(union all)在视图 v 中存在,而在表 emp中没有的行
12、十五、识别和消除笛卡尔积在 from 子句对表进行联接来返回正确的结果集:select e.ename,d.loc from emp e,dept d where e.deptno=10 and d.deptno=e.deptno 十六、聚集与联接mysql/postgresqlselect deptno,sum(distinct sal)as total_sal,sum(bonus)as total_bonus from(select e.empno,e.ename,e.sal,e.deptno,e.sal*case when eb.type=1 then.1 when eb.type=2
13、then.2 else.3 end as bonus from emp e,emp_bonus eb where e.empno=eb.empno and e.deptno=1)x group by deptno db2/oracle/sqlserver select distinct deptno,total_sal,total_bonus from(select e.empno,e.ename,sum(distinct e.sal)over(partition by e.deptno)as total_sal,e.deptno,sum(e.sal*case when eb.type=1 t
14、hen.1 when eb.type=2 名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 29 页 -then.2 else.3 end)over(partition by deptno)as total_bonus from emp e,emp_bonus eb where e.empno=eb.empno and e.deptno=10)x 十七、聚集与外联接db2/mysql/postgresql/sqlserver select deptno,sum(distinct sal)as total_sal,sum(bonus)as total_bonus from(selec
15、t e.empno,e.ename,e.sal,e.deptno,e.sal*case when eb.type is null then 0 when eb.type=1 then.1 when eb.type=2 then.2 else.3 end as bonus from emp e left outer join emp_bonus eb on(e.empno=eb.empno)where e.deptno=10)group by deptno select distinct deptno,total_sal,total_bonus from(select e.empno,e.ena
16、me,sum(distinct e.sal)over(partition by e.deptno)as total_sal,e.deptno,sum(e.sal*case when eb.type is null then 0 when eb.type=1 then.1 when eb.type=2 then.2 else.3 end)over(partition by deptno)as total_bonus from emp e left outer join emp_bonus eb on(e.empno=eb.empno)where e.deptno=10)x oracle sele
17、ct deptno,sum(distinct sal)as total_sal,sum(bonus)as total_bonus from(select e.empno,e.ename,e.sal,e.deptno,e.sal*case when eb.type is null then 0 when eb.type=1 then.1 when eb.type=2 then.2 else.3 end as bonus from emp e,emp_bonus eb where e.empno=eb.empno(+)and e.deptno=10)group by deptno 十八、从多个表中
18、返回丢失的数据db2/mysql/postgresql/sqlserverselect d.deptno,d.dname,e.ename from dept d full outer join emp e on(d.deptno=e.deptno)名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 29 页 -select d.deptno,d.dname,e.ename from dept d right outer join emp e on(d.deptno=e.deptno)union select d.deptno,d.dname,e.ename from dept d l
19、eft outer join emp e on(d.deptno=e.deptno)oracleselect d.deptno,d.dname,e.ename from dept d,emp e where d.deptno=e.deptno(+)union select d.deptno,d.dname,e.ename from dept d,emp e where d.deptno(+)=e.deptno 十九、在运算和比较时使用null值select ename,comm from emp where coalesce(comm,0)(select comm from emp where
20、 ename=WARD)二十、从一个表向另外的表中复制行insert into dept_east(deptno,dname,loc)select deptno,dname,loc from dept where loc in(NEW YORK,BOSTON)二十一、复制表定义db2create table dept_2 like dept oracle/mysql/postgresqlcreate table dept_2 as select*from dept where 1=0 sqlserverselect*into dept_2 from dept where 1=0 二十二、一次向
21、多个表中插入记录oracleinsert all when loc in(NEW YORK,BOSTON)then into dept_east(deptno,dname,loc)values(deptno,dname,loc)名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 29 页 -when loc=CHICAGO then into dept_mid(deptno,dname,loc)values(deptno,dname,loc)else into dept_west(deptno,dname,loc)values(deptno,dname,loc)select dept
22、no,dname,loc from dept db2insert into(select*from dept_west union all select*from dept_east union all select*from dept_mid)select*from dept mysql/postgresql/sqlserver不支持多表插入操作二十三、阻止对某几列插入在表中创建一个视图,该视图将只显示允许用户进行操作的列,强制所有的插入操作都通过该视图进行create view new_emps as select empno,ename,job from emp db2/mysqlupd
23、ate emp e set(e.sal,m)=(select ns.sal,ns.sal/2 from new_sal ns where ns.deptno=e.deptno)where exists(select nul from new_sa ns where ns.deptno=e.deptno)oracleupdate(select e.sal as emp_sal,m as emp_comm,ns.sal as ns_sal,ns.sal/2 as ns_comm frm emp e,new_sal ns where e.deptno=ns.deptno)set emp_sal=ns
24、_sal,emp_comm=ns_comm postgresqlupdate emp set sal=ns.sal,comm=ns.sal/2 from new_sal ns where ns.deptno=emp.deptno 名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 29 页 -sqlserverupdate e set e.sal=ns.sal,m=ns.sal/2 from emp e,new_sal ns where ns.deptno=emp.deptno 二十五、合并记录oraclemerge into emp_commission ec using(sele
25、ct*from emp)emp on(ec.empno=emp.empno)when matched then update set m=1000 delete where(sal=3)(以下模式名 schema为 smeagol)二十九、列出模式中的表db2select tabname from syscat.table where tabschema=smeagol 名师资料总结-精品资料欢迎下载-名师精心整理-第 9 页,共 29 页 -oracleselect table_name from all_tables where owner=smeagol postgresql/mysql
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年数据库sql语句大全参照 2022 数据库 sql 语句 大全 参照
限制150内