zoukankan      html  css  js  c++  java
  • ylb:子查询(嵌套子查询)和子查询(相关子查询)

    ylbtech-SQL Server:SQL Server-子查询(嵌套子查询)和子查询(相关子查询)

     SQL Server 子查询(嵌套子查询)和子查询(相关子查询)。

    1,ylb:1,子查询(嵌套子查询)返回顶部
    -- =============================================
    -- ylb:本案例的目的是:“嵌套子查询”
    -- 11:25 2011/12/9
    -- =============================================
    use pubs
    go
    --一、子查询
    --1,嵌套子查询
    ---特点:in里面的查询语句可以独立运行。
    --P1:查询图书名是“Net Etiquette”的作者是谁?
    --分析
    select title_id from titles where title='Net Etiquette'
    --title_id='PC9999'
    go
    select au_id from titleauthor
    where title_id='PC9999'
    --au_id='486-29-1786'
    go
    --小结
    select au_id from titleauthor
    where title_id in(select title_id from titles where title='Net Etiquette')
    go
    select * from authors 
    where au_id='486-29-1786'
    --总结
    select * from authors 
    where au_id in(select au_id from titleauthor
    where title_id in(select title_id from titles where title='Net Etiquette'))
    
    --测试1,
    --P2:查看图书编号是‘PC9999’的出版社名称是?
    select pub_id from titles
    where title_id='PC9999'
    go
    select pub_name from publishers
    where pub_id='1389'
    go
    --结论
    select pub_name from publishers
    where pub_id in(select pub_id from titles
    where title_id='PC9999')
    go
    View Code
    1,ylb:2,子查询(相关子查询) 返回顶部
    -- =============================================
    -- ylb:本案例探讨的是:“相关子查询”
    -- 11:25 2011/12/9
    -- =============================================
    use pubs
    go
    --一、相关子查询
    --P1:查询出版社和商店在同一个州的商店名称?
    go
    select * from publishers
    select * from stores
    go
    --结论
    select stor_name from stores s
    where state in(select state from publishers where state=s.state)
    
    go
    --P2:查询出版社和作者在同一个的作者姓名?
    
    select * from publishers
    select * from authors
    go
    --结论
    select * from authors a
    where state in (select state from publishers where state=a.state)
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Sql:主表与子表的最新记录级联查询
    发现eclipse红叉,查看markers发现Target runtime Apache Tomcat 6.0 is not defined
    The required Server component failed to start so Tomcat is unable to start问题解决
    Spring官网下载dist.zip的几种方法
    SPRING---------配置文件的命名空间
    eclipse中egit插件使用
    eclipse不自动弹出提示(alt+/快捷键失效)
    使用jenkins配置.net mvc网站进行持续集成
    windows系统的便签
    Linq to Entity 动态拼接查询条件(重点是OR)
  • 原文地址:https://www.cnblogs.com/ylbtech/p/3494588.html
Copyright © 2011-2022 走看看