1

因此,我遇到了从查询中获取正确数据的问题。

我有下表(人):

Name  |       xpath        |   value
Derek |  /body/torso/arm   |   left
Derek |  /body/torso/arm   |   right
Derek |  /body/torso/neck  |   Head
Ron   |  /body/torso/neck  |   Head
Ron   |  /body/torso/arm   |   left

所以基本上我试图得到的查询结果是显示差异(两个人之间缺少什么)。

results:
Name1 |       xpath1       |   value1   | Name2  |   xpath2         |   value2
Derek |  /body/torso/arm   |   right    | Ron    |    NULL          |   NULL

我什至可以将以下内容取回

results:
Name1 |       xpath1       |   value1   | Name2  |   xpath2         |   value2
Derek |  /body/torso/arm   |   right    | Ron    |    NULL          |   NULL
Derek |  /body/torso/arm   |   left     | Ron    | /body/torso/arm  |   left  
Derek |  /body/torso/neck  |   Head     | Ron    | /body/torso/neck |   Head    

我使用的查询是:

SELECT P.Name , P.xpath, P.value, P1.Name, P2.xpath, P3.value
FROM Person as P
LEFT OUTER JOIN
    Person as P2 ON P.xpath = P2.xpath
WHERE
         P.Name = "Derek"
    AND  P2.Name = "Ron"

无论我尝试什么,我不断得到的结果基本上是重叠的数据,但从来不是我真正想要的,似乎我错过了一些简单的东西或者只是做错了。有什么建议么?最终,我将在 SPROC 中运行它,但能够处理多个名称不仅仅是 2,而且如果我有,Derek、Ron、John、Dawn 等。

4

2 回答 2

2

试一试:

SELECT    a.Name  AS Name1,
          a.xpath AS xpath1,
          a.value AS value1,
          'Ron'   AS Name2,
          b.xpath AS xpath2,
          b.value AS value2
FROM      Person a
LEFT JOIN Person b ON b.Name = 'Ron'
                  AND a.xpath = b.xpath 
                  AND a.value = b.value
WHERE     a.Name = 'Derek' AND
          b.Name IS NULL

SQLFiddle 演示


如果您想将 Derek 与多个名称进行比较,您可以CROSS JOIN对所有名称进行比较(以便名称可以出现在结果集中),以及使用,LEFT JOIN的同一个表,以及在 中指定的名称:xpathvalueCROSS JOIN

SELECT     a.Name  AS Name1,
           a.xpath AS xpath1,
           a.value AS value1,
           b.Name  AS Name2,
           c.xpath AS xpath2,
           c.value AS value2
FROM       Person a
CROSS JOIN (
           SELECT 'Ron' AS Name UNION ALL
           SELECT 'John'        UNION ALL
           SELECT 'Charles'     UNION ALL
           SELECT 'Aaron'
           ) b
LEFT JOIN  Person c ON b.Name  = c.Name 
                   AND a.xpath = c.xpath
                   AND a.value = c.value
WHERE      a.Name = 'Derek' AND
           c.Name IS NULL

SQLFiddle 演示


如果您想在比较的左侧有多个名称(不仅仅是“Derek”),只需更改

a.Name = 'Derek'

a.Name <> b.Name

在上面的查询中。


SQLFiddle 演示


编辑:只是为了更进一步:

将所有名称与所有名称进行比较:

SELECT     a.Name  AS Name1,
           a.xpath AS xpath1,
           a.value AS value1,
           b.Name  AS Name2,
           c.xpath AS xpath2,
           c.value AS value2
FROM       Person a
CROSS JOIN (SELECT DISTINCT Name FROM Person) b
LEFT JOIN  Person c ON b.Name  = c.Name 
                   AND a.xpath = c.xpath
                   AND a.value = c.value
WHERE      a.Name <> b.Name AND 
           c.Name IS NULL
于 2012-08-16T17:36:20.370 回答
0

尝试使用 EXCEPT 联合:

WITH tbl (name, xpath, value) as
    (values
        ('Derek','/body/torso/arm','left'),
        ('Derek','/body/torso/arm','right'),
        ('Derek','/body/torso/neck','Head'),
        ('Ron','/body/torso/neck','Head'),
        ('Ron','/body/torso/arm','left')
    )
SELECT xpath, value 
FROM tbl 
WHERE name = 'Derek'
    EXCEPT
        SELECT xpath, value 
        FROM tbl 
        WHERE name = 'Ron'
于 2012-08-16T18:15:28.267 回答