查询规范化表

时间:2015-03-06 11:09:38

标签: sql

我的数据库中有以下表格:

KEY         FIELDID CONTENT
TEST0002338 26      rcTRI150003
TEST0002338 25      COOKIE
TEST0002338 24      w008760
TEST0002339 26      rcTRI150003
TEST0002339 25      COOKIE
TEST0002339 24      w008761
TEST0002340 26      rcTRI150003
TEST0002340 25      COOKIE
TEST0002340 24      w008762
TEST0002341 26      rcTRI150003
TEST0002341 25      COOKIE
TEST0002341 24      w008763
TEST0002342 26      rcTRI150003
TEST0002342 25      COOKIE
TEST0002342 24      w008764
TEST0002343 26      rcTRI150003
TEST0002343 25      COOKIE
TEST0002343 24      w008765

现在我正在尝试编写一个查询来检索KEY,如果我知道三个CONTENT及其对应的FIELDID,就像这样(应该返回TEST0002338 ):

SELECT [KEY]
  FROM [ENTRYFLD]
 WHERE ((("FIELDID"='24') AND ("CONTENT"='w008760')) AND 
       (("FIELDID"='25') AND ("CONTENT"='COOKIE'))) AND 
       (("FIELDID"='26') AND ("CONTENT"='rcTRI150003'))

我还尝试了一些与OR的其他组合,但这也没有用。执行此操作的正确方法是什么(最好只使用非常简单的命令,如ANDOR,因为我不直接编写SQL,而是使用自定义包装器)

4 个答案:

答案 0 :(得分:2)

最简单的方法是使用自联接:

SELECT t1."KEY"
  FROM ENTRYFLD t1
  JOIN ENTRYFLD t2 ON t1."key" = t2."key"
  JOIN ENTRYFLD t3 ON t1."key" = t3."key"
 WHERE (t1."FIELDID"='24' AND t1."CONTENT"='w008760') 
   AND (t2."FIELDID"='25' AND t2."CONTENT"='COOKIE') 
   AND (t3."FIELDID"='26' AND t3."CONTENT"='rcTRI150003')

或者在没有显式连接的情况下编写:

SELECT t1."KEY"
  FROM ENTRYFLD t1, ENTRYFLD t2, ENTRYFLD t3 
 WHERE t1."key" = t2."key"
   AND t1."key" = t3."key"
   AND (t1."FIELDID"='24' AND t1."CONTENT"='w008760') 
   AND (t2."FIELDID"='25' AND t2."CONTENT"='COOKIE') 
   AND (t3."FIELDID"='26' AND t3."CONTENT"='rcTRI150003')

或者你可以使用一些相关的存在谓词(在任何情况下都可能是最佳选择):

SELECT t1."KEY"
FROM ENTRYFLD t1
WHERE EXISTS (
    SELECT 1 
    FROM ENTRYFLD t2 
    WHERE t1."KEY" = t2."KEY" AND (t2."FIELDID"='25' AND t2."CONTENT"='COOKIE')
    )
AND EXISTS (
    SELECT 1 
    FROM ENTRYFLD t3 
    WHERE t1."KEY" = t3."KEY" AND (t3."FIELDID"='26' AND t3."CONTENT"='rcTRI150003')
)
AND (t1."FIELDID"='24' AND t1."CONTENT"='w008760') 

答案 1 :(得分:1)

现在更新了,没有完全理解问题...选择可能的密钥,检查该密钥是否至少有3行!

select "KEY" from
(SELECT DISTINCT "KEY", "FIELDID", "CONTENT"
 FROM "ENTRYFLD"
 WHERE ("FIELDID"='24' AND "CONTENT"='w008760') OR
       ("FIELDID"='25' AND "CONTENT"='COOKIE')  OR 
       ("FIELDID"='26' AND "CONTENT"='rcTRI150003')
)
group by "KEY"
HAVING COUNT(*) >= 3

更新版本,更简化:

select "KEY" from
FROM "ENTRYFLD"
WHERE ("FIELDID"='24' AND "CONTENT"='w008760') OR
      ("FIELDID"='25' AND "CONTENT"='COOKIE')  OR 
      ("FIELDID"='26' AND "CONTENT"='rcTRI150003')
group by "KEY"
HAVING COUNT(DISTINCT "FIELDID") >= 3

答案 2 :(得分:0)

如果您不能在包装器中使用Join,并且如果您可以在where子句中使用EXISTS,请执行以下操作:

SELECT t.[KEY]
  FROM table_name t
 WHERE EXISTS (select * from  table_name  t1  where t1.[KEY] = t.[KEY] AND ( t1."FIELDID"='24' AND t1."CONTENT"='w008760')) 
   AND EXISTS (select * from  table_name  t2  where t2.[KEY] = t.[KEY] AND ( t2."FIELDID"='25' AND t2."CONTENT"='COOKIE'))
   AND EXISTS (select * from  table_name  t3  where t3.[KEY] = t.[KEY] AND ( t3."FIELDID"='26' AND t3."CONTENT"='rcTRI150003'))

答案 3 :(得分:0)

您正在寻找与所有三个字段匹配的密钥:

SELECT [KEY]
FROM [ENTRYFLD]
WHERE (([FIELDID]='24' AND [CONTENT]='w008760')
    OR ([FIELDID]='25' AND [CONTENT]='COOKIE')
    OR ([FIELDID]='26' AND [CONTENT]='rcTRI150003'))
GROUP BY [KEY]
HAVING COUNT(*) = 3

或者,如果您使用的是SQL Server,则可以PIVOT代替:

SELECT P.[KEY]
FROM (SELECT [KEY], [FIELDID], [CONTENT] FROM [ENTRYFLD]) A
PIVOT (MIN ([CONTENT]) FOR [FIELDID] IN ([24],[25],[26])) AS P
WHERE P.[24] = 'w008760'
AND P.[25] = 'COOKIE'
AND P.[26] = 'rcTRI150003'