SQL:交叉连接查询

时间:2015-03-20 09:08:45

标签: sql sql-server cross-join

SELECT * FROM training.dbo.[PERSON] P
LEFT JOIN training.dbo.PERSON_CAREER_HISTORY PC ON (P.PERSON_ID=PC.PERSON_ID)
CROSS JOIN (SELECT DISTINCT PC2.POSITION training.dbo.PERSON_CAREER_HISTORY) PC2 
WHERE PC.POSITION IS NULL

交叉连接部分无法发挥错误 “'。'附近的语法不正确。”

我无法解决它,并且已经修复了大约一个小时。请告诉我我的错误

3 个答案:

答案 0 :(得分:0)

您错过了FROM中的CROSS JOIN

SELECT * FROM training.dbo.[PERSON] P
LEFT JOIN training.dbo.PERSON_CAREER_HISTORY PC ON (P.PERSON_ID=PC.PERSON_ID)
CROSS JOIN (SELECT DISTINCT PC2.POSITION FROM training.dbo.PERSON_CAREER_HISTORY) PC2 
WHERE PC.POSITION IS NULL

答案 1 :(得分:0)

您错过了子查询中的FROM关键字。试试这个:

SELECT 
    * 
FROM 
    training.dbo.[PERSON] P
    LEFT JOIN training.dbo.PERSON_CAREER_HISTORY PC 
        ON (P.PERSON_ID=PC.PERSON_ID)
    CROSS JOIN (
        SELECT DISTINCT 
            PC2.POSITION 
        FROM
            training.dbo.PERSON_CAREER_HISTORY) PC2 
WHERE 
    PC.POSITION IS NULL

答案 2 :(得分:0)

如果您正在寻找人们所拥有的职位,那么这就是您想要的查询:

SELECT *
FROM training.dbo.[PERSON] P CROSS JOIN
     (SELECT DISTINCT PC2.POSITION FROM training.dbo.PERSON_CAREER_HISTORY
     ) pp LEFT JOIN
     training.dbo.PERSON_CAREER_HISTORY PCH
     ON P.PERSON_ID = PC.PERSON_ID AND pp.POSITION = PC.POSITION
WHERE PC.POSITION IS NULL;

join在您的版本中不正确(以及子查询的问题)。否则,我无法弄清楚原始查询的目的。