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

    子查询:查询的结果当做条件使用

    使用数据库,创建两个表:

    use fruit
    go
    create table a
    (
    code int primary key identity(1001,1),
    name varchar(10),
    xuanke int,
    )
    go
    create table b
    (
    code int primary key identity(1,1),
    kemu varchar(10),
    name varchar(10),
    age int,
    )
    go

    在表中添加数据:

    insert into a values('学生a',1)
    insert into a values('学生b',2)
    insert into a values('学生c',1)
    insert into a values('学生d',3)
    insert into a values('学生e',1)
    insert into a values('学生f',4)
    insert into a values('学生i',5,17)
    select * from a
    insert into b values('数学','老师a',34)
    insert into b values('语文','老师b',25)
    insert into b values('英语','老师c',30)
    insert into b values('物理','老师d',45)
    insert into b values('数学','老师e',20)
    select * from b

    开始子查询:

    select * from b
    --选a老师教的课
    select * from b where name='老师a'
    --年龄最小的老师的课
    select * from b where age=(select MIN(age)from b)
    --某个学生选的那个老师,什么课,老师多少岁
    select code,name,(select kemu from b where a.xuanke=b.code),
    (select name from b where a.xuanke=b.code),
    (select age from b where a.xuanke=b.code) from a where name='学生a'
    --选了老师a的课的学生都叫什么名字
    select name from a where xuanke in(select code from b where name='老师a')
    --学生表添加一个年龄表
    alter table a 
    add age int
    --更改年龄数据
    update a set age=23 where code=1001
    update a set age=20 where code=1002
    update a set age=21 where code=1003
    update a set age=16 where code=1004
    update a set age=18 where code=1005
    update a set age=34 where code=1006
    --选比自己年龄小的老师教的课
    select * from b where age<(select age from a where name='学生f')
    --所有选择数学的学生的信息
    select * from a where xuanke in(select code from b where kemu='数学')
    --老师a的学生里年龄最小的
    select* from a where age=(select MIN(age) from a where xuanke in ( select code from b where name='老师a'))
    --选择老师年龄大于30的学生的信息
    select * from a where xuanke in(select code from b where age>30)

    子查询练习:

    查询销售部里的年龄大于35岁的人的所有信息:

    haha表中部门的所有数字代码转换为bumen表中的字符串显示:

    haha表中部门的所有数字代码转换为bumen表中的字符串显示,并且显示每个人的主管:

  • 相关阅读:
    【转】java面试题与答案
    【Appium】移动端自动化测试,触摸(TouchAction) 与多点触控(MultiAction)
    【转载】appium+python自动化-首次打开app权限弹窗问题
    【转载】视频直播平台测试
    关于http协议
    🍖MVC 与 MVT
    🍖Django框架之视图层(CBV源码剖析) ⭐
    🍖Django之settings源码分析
    🍖CGI、FastCGI、WSGI、uWSGI、uwsgi关系
    🐍Python内置函数
  • 原文地址:https://www.cnblogs.com/wy1992/p/6077597.html
Copyright © 2011-2022 走看看