一个表格多个值匹配出另一个字段的值是多少

图片说明

怎么查出满足tagID=5 and tagid=2 and tagid=17 and tagid=19
得到 tagrelationID=1

问题描述的不是很清楚。这个表里没有满足tagID=5 and tagid=2 and tagid=17 and tagid=19的数据。还有你想要得出一个什么结果

PIVOT 了解一下

;WITH temp_table AS(
      SELECT 1 ID,1 TagRelationID,5 Tag_ID
      UNION ALL  
      SELECT 2,1,2
      UNION ALL  
      SELECT 3,1,17
      UNION ALL  
      SELECT 4,1,19
      UNION ALL  
      SELECT 5,1,20
      UNION ALL  
      SELECT 6,1,21
      UNION ALL  
      SELECT 7,2,5
      UNION ALL  
      SELECT 8,2,2
      UNION ALL  
      SELECT 9,2,17
      UNION ALL  
      SELECT 10,2,22
)
SELECT * FROM (
SELECT 
        TagRelationID ,
        MAX([2]) [col_2] ,
        MAX([5]) [col_5] ,
        MAX([17]) [col_17] ,
        MAX([19]) [col_19]
FROM    temp_table t PIVOT( MAX(Tag_ID) FOR Tag_ID IN ( [2], [5], [17], [19] ) ) y
GROUP BY TagRelationID
)t WHERE t.col_2=2 AND t.col_5=5 AND t.col_17=17 AND t.col_19=19;

你是想要 tagID=5 and tagid=2 and tagid=17 and tagid=19 并且tagrelationID=1的数据吗?
那按照我的理解:
SELECT * FROM TblP_TagRelation WHERE TagID IN (2,5,17,19) and TagrelationID=1;就可以了 (●'◡'●)