执行脚本
SQL> @ d:\test.sql;回车
查询包含指定字符串的记录
select * from synonyms where instr(key,'冰')>0;
查询所有sequences
select * from user_sequences
查询oralce主键(序列号)
select BJPRODUCT_SEQUENCE.NEXTVAL from dual
组函数,与group by
select max(id),max(name), bcn from product where brandid=1958778 group by bcn;
//得到的结果集是一行,但max(id),max(name)不是来自一个记录的
select count(cname),cname from brand having count(cname)>1 group by cname;
distinct过滤掉重复的值
select count(distinct name) from brand group by name//组函数+字段组合查询时,group by
比较区别,与下面2个
select count(distinct name) from brand;
select count(name) from brand;
转义字符&
insert和update 时,用'||chr(38)||'代替'&'。
即'A&T',改成'A'||chr(38)||'T',
其中“||”是连接字符串的运算符
常用函数
length()
substr(name,1,2)==substr(name,0,2)推荐用(name,1,2)
截取字符串name,从第一个字符开始算起,长度为2。[a,a+2)
replace(name,(substr(name,4,1)),'昇')
instr(key,'冰')>0;
key字段值中包含'冰'的
此时等效于where key like '%冰%'
函数可以嵌套使用
update brand2 set cname=substr(cname,0,-4) where cname like'%uuuu'//2起始位,3长度都可为负数
select count(cname),cname from brand having count(cname)>1 group by cname
select distinct b.cname b_cname,p.bcn p_bcn from brand b,product p where p.brandid=b.id and INSTR(trim(p.bcn),trim(b.cname))=0
分页查询:
SELECT * FROM (
SELECT b.*,rownum num FROM brand b WHERE rownum<61
) WHERE num>50;
//必须先"<"或"<=",然后才能">"可能这样设计效率高些。
连接查询
内连接
Employee.emp_deptid = Department.deptid
左外连接Employee.emp_deptid = Department.deptid(+)//#############################
左外最常用。比如参数,参数值,参数可能没有参数值。
右连接Employee.emp_deptid(+) = Department.deptid//等号右侧的所有记录均会被显示
全外连接select * from param p FULL OUTER JOIN category c on (p.categoryid=c.id) where p.id>10
select * from (
select a1,count(*) from A1 group by a1,
select a2,count(*) from A2 group by a2,
select a3,count(*) from A3 group by a3,)order by 2
)
//2 在这里的意思是按照第二列的值排序#################