表D结构如下
   Company nvarchar(2)  --公司  主健
Agencyid nvarchar(10) --工号  主健
Name nvarchar(20) –姓名
Dtmonth int –月份  主健
Duty nvarchar(20)-职级
表E结构如下:
Province nvarchar(20)—省公司 ,
Company nvarchar(2) –公司 主健
Adearea nvarchar(8) –业务区部 主健
表D与表E通过company关联,要求编写SQL语句查询每个工号所属的省公司。

解决方案 »

  1.   

    select name,provice 
    from d,e
    where d.company=e.company
      

  2.   

    select d.Agencyid , e.Province
    from d , e
    where d.Company  = e.Company 
      

  3.   

    select d.name,e.provice from d join e on d.company=e.company
      

  4.   

    select d.name,e.provice from d join e on d.company=e.company
      

  5.   

    select distinct agencyid,province
    from d,e where d.company=e.company
      

  6.   

    select d.name,e.provice from d join e on d.company=e.company
      

  7.   

    if object_id('tempdb.dbo.#TA') is not null drop table #TA
    go 
    create table #TA
    (
    Company nvarchar(2),  --公司  主健 
    Agencyid nvarchar(10), --工号  主健 
    [Name] nvarchar(20), --姓名 
    Dtmonth int, --月份  主健 
    Duty nvarchar(20),--职级 
    primary key(Company,Agencyid,Dtmonth)
    )
    insert #TA
    select 'A','001','zhangsan',6,'乡长' union all
    select 'B','002','lisi',7,'厅长' union all
    select 'c','003','wangwu',8,'省长'if object_id('tempdb.dbo.#TB') is not null drop table #TB
    go 
    create table #TB
    (
    Province nvarchar(20) ,--省公司 , 
    Company nvarchar(2) ,--公司 主健 
    Adearea nvarchar(8) ,--业务区部 主健 
    )
    insert #TB
    select '四川','A','销售' union all
    select '广安','B','万家雪' union all
    select '震天','dafa','在售'select #TA.Agencyid , #TB.Province
    from #TA,#TB
    where #TA.Company  = #TB.Company 
      

  8.   


    /*
    子查询的特殊操作符有 
    1 行 – 1 列:使用“=”、“<”、“>” 和其他比较操作符
    1 行 – 多列:使用“EXISTS”
    多行 – 1 列:使用“ANY”、“ALL”、“IN”、“EXISTS”
    多行 – 多列:使用“EXISTS”通配符可用于指定更宽范围的搜索条件 
    使用Exists关键字
    Exists关键字的作用是用来检查在子查询中是否有结果返回。如果有结果返回则为真;如果无结果返回则为假。
    */
    use stuDB
    go
    --如果有笔试或机试成绩为空,则输出全体学员信息
    Select * From stuinfo Where Exists (Select * From stuMarks Where writtenExam is NULL or labExam is NULL)
    --查询笔试或机试成绩为空的学员信息
    Select * From stuinfo Where stuNo in (Select stuNo  From stuMarks Where writtenExam is NULL or labExam is NULL)
    --查询未参加笔试或机试成绩的学员信息
    Select * From stuinfo Left Outer Join stus
    On stuInfo.stuNo=stuMarks.stuNo Where writtenExam is NULL or labExam is NULL
      

  9.   

    [code/*
    子查询的特殊操作符有 
    1 行 – 1 列:使用“=”、“<”、“>” 和其他比较操作符
    1 行 – 多列:使用“EXISTS”
    多行 – 1 列:使用“ANY”、“ALL”、“IN”、“EXISTS”
    多行 – 多列:使用“EXISTS”通配符可用于指定更宽范围的搜索条件 
    使用Exists关键字
    Exists关键字的作用是用来检查在子查询中是否有结果返回。如果有结果返回则为真;如果无结果返回则为假。
    */
    use stuDB
    go
    --如果有笔试或机试成绩为空,则输出全体学员信息
    Select * From stuinfo Where Exists (Select * From stuMarks Where writtenExam is NULL or labExam is NULL)
    --查询笔试或机试成绩为空的学员信息
    Select * From stuinfo Where stuNo in (Select stuNo  From stuMarks Where writtenExam is NULL or labExam is NULL)
    --查询未参加笔试或机试成绩的学员信息
    Select * From stuinfo Left Outer Join stus
    On stuInfo.stuNo=stuMarks.stuNo Where writtenExam is NULL or labExam is NULL=SQL][/code]