zoukankan      html  css  js  c++  java
  • 嵌套查询和分页查询

    1.子查询概念

    (1)就是在查询的where子句中的判断依据是另一个查询的结果,

    如此就构成了一个外部的查询和一个内部的查询,这个内部的查询就是自查询。

    (2)自查询的分类

    1.独立子查询 (查询的结果用=接收,只有一个值):

     独立单值(标量)子查询(=)

    Select

    testID,stuID,testBase,testBeyond,testPro

    from Score

    where stuID=(

    select stuID from Student where stuName='Kencery'

    )

     独立多值子查询(in) (查询的结果有多个值,用 in not in接收)

    Select

    testID,stuID,testBase,testBeyond,testPro

    from Score

    where stuID in(

    select stuID from Student where stuName='Kencery'

    )

    2)相关子查询 (外部查询和内部查询同时进行的查询,外部为内部子查询提供了条件元素)

    (3)写子查询的注意事项

    1.子查询用一个圆括号括起来,有必要的时候需要为表取别名,使用“as 名字”即可。

    2.表连接

    (1)表链接就是将多个表合成为一个表,但是不是向union一样做结果集的合并操作,

    但是表链接可以将不同的表合并,并且共享字段。

    (2)表连接之交叉连接 (cross join)

    1)创建两张表

    use Test

    go

    create table testNum1

    (

    Num1 int

    );

    create table testNum2

    (

    Num2 int

    );

    insert into testNum1 values(1),(2),(3)

    insert into testNum2 values(4),(5)

    2) 执行交叉连接的SQL语句

    select * from testNum1 cross join testNum2

    3)注解 交叉连接就是将第一张表中的所有数据与第二张表中的所有数据挨个匹配一次构成一个新表

    4)自交叉的实现 执行插入SQL语句:

    insert into testNum1 values(4),(5),(6),(7),(8),(9),(0)

    (3)表连接之内连接

    1)内链接是在交叉连接的基础之上添加一个约束条件

    2)语法:select * from 表1 inner join 表2 on 表1.字段=表2.字段

    Selects1.stuID,

    s1.stuName,

    s1.stuSex,

    s2.testBase,

    s2.testBeyond

    from Student as s1

    inner join Score as s2

    on s1.stuID=s2.stuID

    where s1.stuIsDel=0;

  • 相关阅读:
    Binary Tree Zigzag Level Order Traversal
    Binary Tree Level Order Traversal
    Symmetric Tree
    Best Time to Buy and Sell Stock II
    Best Time to Buy and Sell Stock
    Triangle
    Populating Next Right Pointers in Each Node II
    Pascal's Triangle II
    Pascal's Triangle
    Populating Next Right Pointers in Each Node
  • 原文地址:https://www.cnblogs.com/6zhi/p/5271232.html
Copyright © 2011-2022 走看看