ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [转]sql语句-005

[转]sql语句-005

原创 Linux操作系统 作者:31597359 时间:2019-07-20 22:51:02 0 删除 编辑
05
44. not in的替代。
一般not in的效率比较低。特别是数据量大的时候,几乎不能执行。
用下面几种方式可以替换写法
比如要查询在fee_rev_info表中已经销户的用户(不在cm_user中的)(不过下面的例子不是很好,因为bill_id是cm_user的唯一索引)
select * from fee_rev_info where bill_id not in (select bill_id from cm_user)
<1> 用not exists
select * from fee_rev_info a where not exists (select 'p' from cm_user b where b.bill_id = a.bill_id)
<2> 用外连接(+)
select a.* from fee_rev_info a,cm_user b
where a.bill_id = b.bill_id (+)
and b.bill_id is null
<3> 用hash_aj
select /*+HASH_AJ*/* from fee_rev_info where bill_id not in (select bill_id from cm_user)

45.怎么样查询特殊字符,如通配符%与_
假如数据库中有表 STATIONTYPE,STATION_571 STATION_572 ...
select * from tab where tname like 'STATION_%'
会显示 STATIONTYPE,STATION_571 ... 可以用下面的语句
select * from tab where tname like 'STATION_%' escape''

46.如果存在就更新,不存在就插入可以用一个语句实现吗
9i已经支持了,是Merge,但是只支持select子查询,
如果是单条数据记录,可以写作select .... from dual的子查询。
语法为:
MERGE INTO table
USING data_source
ON (condition)
WHEN MATCHED THEN update_clause
WHEN NOT MATCHED THEN insert_clause;


MERGE INTO cm_user_credit
USING (select * from dual) ON (user_id =1302514690 )
when MATCHED then update set credit_value = 1000
when NOT MATCHED then insert (user_id,acc_id,bill_id,plan_id,region_code,credit_value) values(1302514690,1305032158,'13857141218',10070247,'571',1000);

47.怎么实现一条记录根据条件多表插入
9i以上可以通过Insert all语句完成,仅仅是一个语句,如:
INSERT ALL
WHEN (id=1) THEN
INTO table_1 (id, name)
values(id,name)
WHEN (id=2) THEN
INTO table_2 (id, name)
values(id,name)
ELSE
INTO table_other (id, name)
values(id, name)
SELECT id,name
FROM a;

如果没有条件的话,则完成每个表的插入,如
INSERT ALL
INTO table_1 (id, name)
values(id,name)
INTO table_2 (id, name)
values(id,name)
INTO table_other (id, name)
values(id, name)
SELECT id,name
FROM a;

48.如何实现行列转换
<1>、固定列数的行列转换

student subject grade
---------------------------
student1 语文 80
student1 数学 70
student1 英语 60
student2 语文 90
student2 数学 80
student2 英语 100
...
转换为
语文 数学 英语
student1 80 70 60
student2 90 80 100
...
语句如下:
select student,sum(decode(subject,'语文', grade,null)) "语文",
sum(decode(subject,'数学', grade,null)) "数学",
sum(decode(subject,'英语', grade,null)) "英语"
from table
group by student

<2>、不定列行列转换

c1 c2
--------------
1 我
1 是
1 谁
2 知
2 道
3 不
...
转换为
1 我是谁
2 知道
3 不

这一类型的转换必须借助于PL/SQL来完成,这里给一个例子
CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER)
RETURN VARCHAR2
IS
Col_c2 VARCHAR2(4000);
BEGIN
FOR cur IN (SELECT c2 FROM t WHERE c1=tmp_c1) LOOP
Col_c2 := Col_c2||cur.c2;
END LOOP;
Col_c2 := rtrim(Col_c2,1);
RETURN Col_c2;
END;
/
SQL> select distinct c1 ,get_c2(c1) cc2 from table;即可


--例子:
create table okcai_1
(
user_id varchar2(10),
user_number varchar2(10),
user_num number(8)
)
user_id user_number user_num
---------------------
1 123 2
1 456 5
1 789 6
2 11 2
2 22 3
2 33 4
2 44 5
2 55 6
2 66 7
2 77 8
3 1234 1
3 5678 2

方式一:
create or replace function get_col(
p_userId number,
p_col number
) return varchar
as
v_tmp varchar2(255);
begin
select user_number||chr(9)||user_num into v_tmp
from
(select user_number,user_num,rownum row_id
from okcai_1
where user_id = p_userId) a
where row_id = p_col;
return ltrim(v_tmp);
--return v_tmp;
end;

然后
select distinct user_id,get_col(user_id,1),get_col(user_id,2),get_col(user_id,3) .... from okcai_1

方式二:
create or replace function get_col(
p_userId number,
p_col number
) return varchar
as
v_tmp varchar2(255);
begin
select user_number||chr(9)||user_num into v_tmp
from
(select user_number,user_num,rownum row_id
from okcai_1
where user_id = p_userId) a
where row_id = p_col;
return ltrim(v_tmp);
--return v_tmp;
end;
select distinct user_id,get_col_new(user_id) from okcai_1;


49.怎么设置存储过程的调用者权限
普通存储过程都是所有者权限,如果想设置调用者权限,请参考如下语句
create or replace
procedure ...()
AUTHID CURRENT_USER
As
begin
...
end;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8000572/viewspace-62279/,如需转载,请注明出处,否则将追究法律责任。

上一篇: [转]sql语句-004
下一篇: [转]sql语句-006
请登录后发表评论 登录
全部评论

注册时间:2006-10-07

  • 博文量
    120
  • 访问量
    77553