1:空表DUAL
只能存放返回一行的数据。
Select sysdate from dual;
Select seq_t_mc.nextval from dual;
2:
单列
Select Nid from T_mc;
Select Nid,sname from t_mc;
所有列
Select * from t_mc;
别名:
可以为字段/表/视图取另一个名称.
Select NID,NID as 类别ID号 from t_mc t;
Select NID,NID 类别ID号 from t_mc t;
常量列
Select '博洋教育' as company,t.* from t_user t
Select 'x','y','z',t.* from t_user t
字段连接符
Select suser || '(' || sname || ')' from t_user
Select concat(concat(concat(suser,'('),sname),')') from t_user
update t_user set sname=sname || spwd where suser = 'admin'
条件Where查询
where后面的条件可以是逻辑表达式。
越精确的条件放在越前面
Select * from t_user where ssex='男' and nage =29
Select * from t_user where nage=29 and ssex='男'
Select * from t_user where ssex='男' or nage <>29
Select * from t_user where ssex='男' or nage !=29
NULL查询
NULL表示值是一个空值,注意与空字符的区别
Select * from t_user where spwd is null;
Select * from t_user where spwd = ' ';
Select * from t_user where spwd is not null;
限制行数查询
Select * from t_user where rownum<=5;
模糊查询Like
Select * from t_user where suser like '%a%' //包含字符a
Select * from t_user where suser like 'a%' //以'a'开头的字符
Select * from t_user where suser like '%a' //以a结尾的字符
Select * from t_user where suser like 'a_m%' //以a开始,第3个字符为m
Select * from t_user where suser like 'a%i%' //以a开始,里面包含i
范围查询Between
Select * from t_user where nage between 21 and 24;
Select * from t_user where nage>=21 and nage<=24;
in/exists查询
in/not in
Select * from t_user where nage in(21,24);
Select * from t_user where nage=21 or nage=24;
in后面跟上一个集合
Delete from t_user where suser in('ab','lili','xuxun')
in后面跟上一个查询结果
Select * from t_mc t where t.nmaxid in
(Select nid from t_mc_type where npid=0)
Delete from t_mc where nmaxid not in
(Select nid from t_mc_type where npid=0)
单字段/多字段排序
select t.*, t.rowid from t_mc t order by sname asc,nprice desc
//如果按照主键排序的,后面的排序将不起作用。
//主键不可能重复。
select t.*, t.rowid from t_mc t order by nid asc,nprice desc
子查询
把一个查询语句嵌套到另一个查询语句中。
注意点:
A:子查询当做一张虚拟表时,必须要对虚拟表设置别名。
B:子查询可以放在Select语句的任意地方。
Select nid,sname,nmaxID,
(Select sname from t_mc_type where nid=nmaxid) as 大类别名称,
nminid,
(Select sname from t_mc_type where nid=nminid) as 小类别名称
from t_mc
where nmaxid in(Select nid from t_mc_type where npid=0)
Select * from (
Select nid,sname,nmaxID,
(Select sname from t_mc_type where nid=nmaxid) as 大类别名称,
nminid,
(Select sname from t_mc_type where nid=nminid) as 小类别名称
from t_mc
where nmaxid in(Select nid from t_mc_type where npid=0)
) t where t.sname like '%联想%'
--Oracle分页语句
Select * from (Select rownum rn,t.* from t_mc t where rownum<=9) t
where t.rn>=3
--这条效率比较高
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM userinfo ) A
WHERE ROWNUM <= 100000
)
WHERE RN >= 99999
SELECT * FROM (
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM userinfo) A
)
WHERE RN BETWEEN 99999 AND 100000
单表查询的例子
浙公网安备 33010602011771号