SQL Server查询返回许多笛卡尔积

时间:2017-02-11 05:27:57

标签: sql sql-server cartesian-product

我有一个SQL Server查询,如下所示:

select 
    ISNULL(UPPER(w.role), '-') as 'Position Title',
    concat ('SGD ',m.expectedSalary) as 'Expected Salary',
    (cast(w.endYear as int) - cast(w.startYear as int)) as 'Experience',
    mq.Qualification as 'Education Level',
    ISNULL(ms.specialisation, '-') as 'Specialisation',
    mj.dateApplied as 'Date of Application'
from 
    WorkExpr w,
    Member m,
    MemberQlftn mq,
    MemberSpln ms,
    MemberJob mj
where 
    mj.jobNumber = (select jobNumber
                    from MemberJob
                    where email = 'alanang@gmail.com')

它应该返回给我申请与alan相同工作的人员的详细信息(例如职位空间,预期工资等)(​​电子邮件是'alanang@gmail.com')。但是,当我运行这个查询时,当我只能回来时,我会获得超过6000行数据4.有人能告诉我我做错了什么吗?谢谢

1 个答案:

答案 0 :(得分:0)

好了,现在你需要用他们的主键/外键上的INNER JOINS替换你的交叉连接表列表:

    from WorkExpr w,
    Member m,
    MemberQlftn mq,
    MemberSpln ms,
    MemberJob mj

替换为以下内容,但表格之间有适当的关系:

    from WorkExpr w
    inner join Member m
        on w.memberid = m.memberid
    inner join MemberQlftn mq 
        on w.memberid = mq.memberid
    inner join MemberSpln ms
        on w.memberid = ms.memberid
    inner join MemberJob mj
        on w.memberid = mj.memberid