Home  >  Article  >  Database  >  一些实用的sql语句

一些实用的sql语句

WBOY
WBOYOriginal
2016-06-07 18:07:18998browse

一些实用的sql,需要的朋友可以参考下。

1.查询高于平均价格的商品名称:
SELECT item_name FROM ebsp.product_market_price WHERE item_price > (SELECT AVG(item_price) FROM ebsp.product_market_price
2.oracle9i以上版本,可以实现将某张表的数据同时插入到多张表中。例:
代码如下:
INSERT ALL
WHEN deptno=10 THEN INTO dept10 --部门编号为10的插入表dept10中
WHEN deptno=20 THEN INTO dept20
WHEN deptno=30 THEN INTO dept30
WHEN job='CLERK' THEN INTO clerk --岗位为CLERK的插入表clerk 中
ELSE INTO other
SELECT * FROM emp;

可以将前面的sql语句变为INSERT FIRST 后面不变,当使用First操作符执行多表插入时,如果数据已经满足了先前的条件,并且已经被插入到某表中,那么该行数据在后续的插入中将不会被再次使用。
3.截取字符串制定的长度。
代码如下:
select substr(item_name,0,2) from ebsp.product_market_price
select substr(‘ho鲜红的鲜花 ',0,3) from dual; --print ‘ho鲜'

获得emp系统表中hiredate雇佣日期,有重复的记录,也就是一天中雇佣多名员工的记录。
代码如下:
SQL1: select * from scott.emp where hiredate in (select hiredate mycount from scott.emp group by hiredate having count(*) >1)
SQL2:select t2.* from scott.emp t2 ,
(select t.hiredate,count(*) mycount from scott.emp t group by t.hiredate having count(*) >1) t1
where t2.hiredate = t1.hiredate

如果hiredate存入数据库中时日期型带有时分秒,可以通过to_char(CREATE_DATE, 'YYYY-MM-DD')来代替上面的
4.修改oracle数据库缓存大小,以system登陆:
代码如下:
alter system set db_cache_size = 700m scope = spfile;
alter system set shared_pool_size = 200m scope=spfile;
alter system set pga_aggregate_target = 100m scope=spfile;
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn