码迷,mamicode.com
首页 > 其他好文 > 详细

查询语句1

时间:2016-11-07 22:17:42      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:加法   开始   表关联   rom   运算   存在   连接   查询条件   null   

查询语法与类型
Select *  from  表名
select  列名1,、列名2、列名3 、列名4  from  表名

 --在数据库中查询数据使用select命令
 --执行数据库查询时,可以使用*表示查询表格所有的列
 select * from libuser;
 
 --执行数据查询时,也可以指定要查询的列
 --select userid,uaccount,passwd,age,regdate,sex from libuser;
 --select userid,uaccount,age,regdate from libuser;

精确查询
Select *  from  表名  where  列名=‘查询条件’

 -使用精确查询
-- 查询数据使用select进行查询, select 列名(多个列可以使用逗号进行分隔或使用*表示所有的列) from 表名 where 条件(列名=查询条件) 
 select * from libuser where uaccount = lisi; 

模糊查询
select  列名1,、列名2  from  表名  where 列名>=查询条件
                     列名 like%123%’   ‘%123’   ‘%12__’
--使用模糊查询
--查询年龄大于等于19岁的人
 select userid,uaccount,passwd,age,regdate,sex from libuser where age >= 22;
 
--查询账号包含有lin的人
select * from libuser where uaccount like %lin%; 

--查询所有以lin开头的人
select * from libuser where uaccount like lin%;
 
--查询所有以ing结尾的人
select * from libuser where uaccount like %ing; 

--模糊查询可以使用like查询实现,可以使用%表示0个或多个字符,还可使用_表示一个不确定的字符
select * from libuser where uaccount like %ing_;
--注意:一条下划线_表示一个不确定的字符,N条下划线表示N个不确定的字符
select * from libuser where uaccount like %in__;


时间查询
select  列名1,、列名2  from  表名  where 列名格式=查询条件

--查询注册时间是6月份的人
select userid,uaccount,age,to_char(regdate,yyyy-mm-dd hh24:mi:ss) from libuser where to_char(regdate,yyyymm)=201606;

--练习:查询指定年月日的人 
 
--日期比较
--找出注册时间在7月13号前的人
--编程语言中的时间是指从1970年1月1号0点0时0分0秒0毫秒到当前时间点的时间差,所以日期越往后值越大;这里使用的数值判断两个数值谁大谁小
select userid,uaccount,age,to_char(regdate,yyyy-mm-dd hh24:mi:ss) from libuser where regdate<to_date(2016-7-13,yyyy-mm-dd);


--练习:查找注册时间在2016-07-12 13:30:52后的人,包含2016-07-12 13:30:52

 --查询星期几--注意每周是从星期天开始的,故星期天的数值是1
 select to_char(regdate,day),to_number(to_char(regdate,D)) from libuser;
 

多条件查询
select  列名1,、列名2  from  表名  where 列名=查询条件 and列名=查询条件
 列名=查询条件 or列名=查询条件   1X(1+0)X1=1   1X(1+0)X0=0
列名=查询条件 or列名=查询条件 and列名=查询条件 or列名=查询条件             
--多条件查询
 --根据用户的账号和密码找出用户的信息
 --需要同时满足两个查询条件的,需要进行逻辑与操作,使用and运算符号实现(如果是多条件都需要满足,使用多个and)
 select userid,uaccount,passwd,age,to_char(regdate,yyyy-mm-dd hh24:mi:ss) from libuser where uaccount=linling and passwd=linling;
 
 
 --查找linling或linling2的数据
 --或查询时逻辑或操作,使用or运算符实现(如果有多个条件使用多个or)
 select userid,uaccount,passwd,age,to_char(regdate,yyyy-mm-dd hh24:mi:ss) from libuser where uaccount=linling or uaccount=linling8
 
 
 --查找出userid为10003且密码等于123456的用户 或者账号为linling且密码等于linling的用户
 --注意:在同一查询条件中,如有and与(乘法)运算,又有or或(加法)运算;这样的运算可以看成算术运算的混合运算.先乘除后加减
 select userid,uaccount,passwd,age,to_char(regdate,yyyy-mm-dd hh24:mi:ss) from libuser 
       where userid=10003 and passwd=123456 or uaccount=linling and passwd=linling;
 --可以使用小括号改变运算的优先级别
  select userid,uaccount,passwd,age,to_char(regdate,yyyy-mm-dd hh24:mi:ss) from libuser 
       where (userid=10003 and passwd=123456) or (uaccount=linling and passwd=linling);
    
  -- 理解逻辑运行的优先级别  
   select userid,uaccount,passwd,age,to_char(regdate,yyyy-mm-dd hh24:mi:ss) from libuser 
       where userid=10003 and (passwd=123456 or uaccount=linling) and passwd=linling;  

非空查询
select  列名1,、列名2  from  表名  where 列名 is null
select  列名1,、列名2  from  表名  where 列名 is not null

  --查询字段为null的数据用 is null
  select userid,uaccount,passwd,age ,to_char(regdate,yyyy-mm-dd hh24:mi:ss) regdate from libuser where age is null;
  --查询字段不为空null的数据用is not null 
  select userid,uaccount,passwd,age ,to_char(regdate,yyyy-mm-dd hh24:mi:ss) regdate from libuser where age is not null;

  
过滤查询
select  列名1,、列名2  from  表名  where 表名in(查询条件)
select  列名1,、列名2  from  表名  where 表名not in(查询条件)

--使用in查询过滤数据
  select userid,uaccount,passwd,age ,to_char(regdate,yyyy-mm-dd hh24:mi:ss) regdate from libuser 
     where userid in (10003,10005 ,100020 ,20000 ,2001 ,200003, 200052); 
 --使用 not in 查询过滤数据
 select userid,uaccount,passwd,age ,to_char(regdate,yyyy-mm-dd hh24:mi:ss) regdate from libuser 
     where userid not in (10003,10005 ,100020 ,20000 ,2001 ,200003, 200052); 


关联查询 子查询 使用join关联查询 
select stationid,stationname,select Areaid from Area where Areaname = 北京市,stationsite, from Station where stationid=1;
  
  select s.stationid,s.stationname,s.stationsite,a.areaname from Station s join area a on s.areaid=a.areaid;
  
  
  --自关联查询
  select a.areaid,a.areaname,a.superior,a2.areaname from Area a left join Area a2 on a.superior = a2.areaid order by a.areaid ;
  --多表关联查询
  select s.stationid,s.stationname,a.areaname,s.stationsite from Station s join Area a on s.areaid=a.areaid order by s.stationid;
  
  ---多表查询,子查询
  select t.trainid,tr.typename,st.stationname z,sta.stationname,t.starttime,t.sndtime,t.mile,t.price from train t join traintype tr on t.tpid=tr.tpid join Station st on st.stationid=t.starstation join Station sta on sta.stationid=t.endstation;
  
 
--左连接查询--会把join左边的表的所有数据都查询出来
select * from goods t left outer join category c on t.category=c.id;

--右连接查询--会把join右边的表的所有数据都查询出来
select * from goods t right outer join category c on t.category=c.id;

--内连接查询--只查询有关联关系的数据
select * from goods t  join category c on t.category=c.id;
select * from goods t inner join category c on t.category=c.id;

  --查找书籍分类是"航空航天3"的书籍
   select * from book where btype=(select bid from booktype where btname=航空航天3);
   
   
   --课后练习:查找书籍分类是"航空航天3"的书籍,要求同时显示分类名称
   --多表联合查询--
   --查找的数据如果保存在多个表中,且这些表有主外键关系,可以通过主外键进关联查询
   
   
   --select 列名 from 外键表,主键表  where 外键表的外键=主键表的主键
   select * from book b,booktype bt where b.btype=bt.bid
   --颠倒表或表关系查询的结果是一样的
   select * from booktype bt,book b where bt.bid=b.btype
   
   
   
   --在查询列时,可以使用表的别名分别制定两个表要查询的列
   select b.*,bt.btname from booktype bt,book b where bt.bid=b.btype
   --使用表的别名指定要查询的列
   select b.bookname,b.price,bt.btname from book b,booktype bt where b.btype=bt.bid;
   
   
   --练习:
   --1.根据书籍类名查询书籍名称(已知书籍类名称"航空航天3",要求查询该类型下的书籍名称); 
   
   --1.1把两张表都在主查询中进行关联查询:满足主外键关系,满足查询条件
   SELECT * FROM BOOK;
   --select b.bookname from book b,booktype bt where   bt.btname=‘航空航天3‘;
   select b.bookname from book b,booktype bt where b.btype=bt.bid and bt.btname=航空航天3;
   
   
   --1.2使用子查询方法
   --select b.bookname from book b where b.btype=16
   --select bid from booktype where btname=‘航空航天3‘
   
   select b.bookname from book b where b.btype=( select bid from booktype where btname=航空航天3);
   
   
   --1.3使用关联查询
   --select 列 from 表  join 关联表 on 关联条件 where 其他查询条件
   select b.bookname from book b join booktype bt on b.btype=bt.bid where bt.btname=航空航天3;
   
   --查询加强:查询isbn为ISBN-2016-NUM23的书名和类型名
   select b.bookname,bt.btname from book b join booktype bt on b.btype=bt.bid where b.isbn=ISBN-2016-NUM23;
   
   --练习:查询价格为"20.02" ,且类型名为"航空航天3"的书名
   select b.bookname from book b join booktype bt on b.btype=bt.bid where b.price=20.02 and bt.btname=航空航天3;
   
   select b.bookname from book b join booktype bt on b.btype=bt.bid and b.price=20.02 and bt.btname=航空航天3;
   
   
   
     
   -- 2.根据书籍名称查询类型名称(已知书籍的名称"oracle项目化编程",要求查询该书籍的类型名称)
   --2.1通过主查询进行关联
   select bt.btname from booktype bt ,book b where bt.bid=b.btype and b.bookname=oracle项目化编程;
   --2.2通过子查询进行关联查询
   select bt.btname from booktype bt where bt.bid = (select btype from book where  bookname=oracle项目化编程);
   --2.3通过join关联查询
   select bt.btname from booktype bt join book b on bt.bid = b.btype where b.bookname=oracle项目化编程;
   

 

查询语句1

标签:加法   开始   表关联   rom   运算   存在   连接   查询条件   null   

原文地址:http://www.cnblogs.com/bingo584235807/p/6040713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!