T-SQL中表元素之间的逻辑AND

时间:2013-05-30 22:18:15

标签: sql sql-server tsql join

我的n个表格都包含相同的字段:UsernameValue。相同的用户名可以在每个表上有多个寄存器,但组合Username/Value在每个表上都是唯一的。

我想将这些表连接成一个表,其中包含所有表中出现所有不同(用户名/值)对的所有用户。

实施例

  • 表A:{(User1,Value1);(User1,Value2);(User2,Value2);(User3,Value4)]

  • 表B:{(User1,Value4);(User3,Value5)]

  • 表C:{(User1,Value5);(User1,Value2);(User2,Value7);(User3,Value8)]

期望的输出

Table D: {(User1,Value1);(User1,Value2);(User1,Value4);(User1,Value5);(User3,Value4);(User3,Value5);(User3,Value8)}

现在我正在进行多次连接(使用perl),就像这样

SELECT * 
INTO $target_table 
FROM (SELECT *  
      FROM $table1 
      WHERE bname IN (SELECT DISTINCT bname FROM $table2) 
      UNION
      SELECT * 
      FROM $table2 
      WHERE bname IN (SELECT DISTINCT bname FROM $table1)
     ) UN

然后在第三个表和target_table之间进行相同的连接,依此类推,但我认为这应该是更好的方法。

任何提示?

2 个答案:

答案 0 :(得分:2)

您可以使用UNION

SELECT username, value
FROM $table1
UNION
SELECT username, value
FROM $table2
...
SELECT username, value
FROM $tablex

这将返回不同的记录。如果您对重复项感兴趣,请使用UNION ALL


根据您的编辑,如果用户在所有表中,您似乎只想返回记录。

打破这种局面,你需要做一些事情。首先,将所有记录再次组合在一起,但这次表示每个记录来自哪个表。然后,您需要知道每个用户所在的表的数量。最后,您需要根据表的总数来检查该数字。

这是使用一些CTE的一种方式:

WITH CTE AS (
  SELECT username, value, 1 AS tbl
  FROM t1
  UNION
  SELECT username, value, 2 AS tbl
  FROM t2
  UNION
  SELECT username, value, 3 AS tbl
  FROM t3
  ),
CTECnt AS (
  SELECT username, COUNT(DISTINCT tbl) tblCnt
  FROM CTE
  GROUP BY username
  ),
CTEMaxCnt AS (
  SELECT COUNT(DISTINCT tbl) MaxCnt
  FROM CTE
  )
SELECT C.username, C.value
FROM CTE C
  JOIN CTECnt C2 ON C.username = C2.username
  JOIN CTEMaxCnt C3 ON C2.tblCnt = C3.MaxCnt

答案 1 :(得分:1)

With Combined As
  (
    Select 'A' As TableName, Username, Value
    From TableA
    Union All
    Select 'B', Username, Value
    From TableB
    Union All
    Select 'C', Username, Value
    From TableC
    )
Select C.Username, C.Value
From Combined As C
    Join (
          Select C1.Username
          From Combined As C1
          Group By C1.Username
          Having Count(Distinct C1.TableName) = 3
          ) As Z
      On Z.Username = C.Username
Group By C.Username, C.Value

SQL Fiddle version