zoukankan      html  css  js  c++  java
  • 20141103--子查询

     1 ---------------------子查询--------------------
     2 --子查询就是将条件信息换成表达式的一种查询方式
     3 --只查出一列,然后把查出的一列作为条件的参数值
     4 select bumen,COUNT(*)人数 from biao2 group by bumen having COUNT(*)>5
     5 --查询人数大于3的部门中年龄最大的人的信息
     6 select *from biao2 where age =(--查询年龄=
     7 select MAX(age) from biao2 where bumen in(--查询部门中年龄最大的
     8 select bumen from biao2 group by bumen having COUNT(*)>5--部门人数大于3的
     9 )
    10 )--查询出了部门人数大于5中年龄最大的人的信息
    11 and bumen in(select bumen from biao2 group by bumen having COUNT(*)>5)--加上部门人数条件>5
    12 --最后就是查询人数大于5的部门中 年龄最大的人的信息
    13 --按年龄排序, 查询第6-8的信息
    14 select *from biao2 order by age 
    15 select top 5 *from biao2 order by age 
    16 select top 3 *from biao2 where [No.] not in(select top 5 [No.] from biao2 order by age)order by age 
    17 --查询男 年龄最大的全部信息
    18 select MAX(age) from biao2 where sex =''--先查询出男性年龄最大的
    19 select *from biao2 where age =(select MAX(age) from biao2 where sex ='')
    20 --查找人数最多的部门中35岁的人的信息
    21 select *from biao2 where age=37
    22 select top 1 bumen from biao2 group by bumen order by COUNT(*) desc--先查出人数最多的部门
    23 select *from biao2 where age=37 and bumen=(select top 1 bumen from biao2 group by bumen order by COUNT(*) desc)
    24 
    25 --分页,每5条信息一页
    26 select top 5 *from biao2 where [No.] not in (select top 0 [No.] from biao2)--第一页
    27 select top 5 *from biao2 where [No.] not in (select top 5 [No.] from biao2)--第二页
    28 select top 5 *from biao2 where [No.] not in (select top 10 [No.] from biao2)--第三页
    29 --查看一个表按照规定多少条一页,分多少页。
    30 select ceiling(COUNT(*)/5.0) from biao2 --ceiling 取上限 注意数据类型必须转成小数型
     1 ---------------------子查询,两个表嵌套-------------------------
     2 create table bumen
     3 (
     4     code int,
     5     name varchar(20),
     6     zhineng varchar(50),
     7     ceo varchar(20),
     8     dianhua varchar(20)
     9 )
    10 go
    11 insert into bumen values(1,'销售部','负责产品销售和策划','张三','13515463258')
    12 insert into bumen values(2,'财务部','负责公司财务','否子','13585204756')
    13 insert into bumen values(3,'生产部','负责产品生产','再三年','13598741236')
    14 insert into bumen values(4,'后勤部','负责产品管理和调控','分离','13585247536')
    15 update biao2 set bumen =1 where bumen='销售部'
    16 update biao2 set bumen =2 where bumen='财务部'
    17 update biao2 set bumen =3 where bumen='生产部'
    18 update biao2 set bumen =4 where bumen='后勤部'
    19 
    20 select *from bumen
    21 select *from biao2
    22 --查询 ‘我二分’ 所在部门信息
    23 select *from bumen where code =(select bumen from biao2 where name='我二分')--括号中先查出‘我二分’部门编号
    24 --查询 ‘张三分’ 手下的员工信息
    25 select *from biao2 where bumen=(select code from bumen where ceo='张三分')--括号中先查出'张三分'所在部门
  • 相关阅读:
    Composite in Javascript
    Model Validation in Asp.net MVC
    HttpRuntime.Cache vs. HttpContext.Current.Cache
    Controller Extensibility in ASP.NET MVC
    The Decorator Pattern in Javascript
    The Flyweight Pattern in Javascript
    Model Binding in ASP.NET MVC
    Asp.net MVC
    jQuery Ajax 实例 全解析
    ASP.NET AJAX入门系列
  • 原文地址:https://www.cnblogs.com/Tirisfal/p/4070825.html
Copyright © 2011-2022 走看看