查询以查找同一表中的差异

时间:2012-08-16 17:32:18

标签: mysql

所以我遇到了从查询中获取正确数据的问题。

我有下表(人):

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等等将是很好的。

2 个答案:

答案 0 :(得分: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 Demo


如果您想将Derek与多个名称进行比较,您可以对要比较的所有名称CROSS JOIN进行比较(以便名称可以显示在结果集中),并LEFT JOIN使用相同的表格使用xpathvalue以及CROSS 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 Demo


如果你想在比较的左边有多个名字(不仅仅是“Derek”),那就改变

a.Name = 'Derek'

a.Name <> b.Name

在上面的查询中。


SQLFiddle Demo


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

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

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

答案 1 :(得分:0)

尝试使用EXCEPT联盟:<​​/ p>

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'