SQL - 使用查询查找包含2个或更多项的事务

时间:2010-12-04 14:22:09

标签: sql ms-access mysql

我有这张桌子:

TABLE Transaction
Trans_ID    Name    Value    Total_Item  
100          I1     0.33333333        3  
100          I2     0.33333333        3  
100          I5     0.33333333        3  
200          I2     0.5               2  
200          I5     0.5               2  
300          I2     0.5               2  
300          I3     0.5               2  
400          I1     0.33333333        3  
400          I2     0.33333333        3  
400          I4     0.33333333        3  
500          I1     0.5               2  
500          I3     0.5               2  
600          I2     0.5               2  
600          I3     0.5               2  
700          I1     0.5               2  
700          I3     0.5               2  
800          I1     0.25              4  
800          I2     0.25              4  
800          I3     0.25              4  
800          I5     0.25              4  
900          I1     0.33333333        3  
900          I2     0.33333333        3  
900          I3     0.33333333        3  
1000         I1     0.2               5  
1000         I2     0.2               5  
1000         I4     0.2               5 

我需要查找包含某个项目的事务。 EX:2项目组合

I1和I2并且只保留相关的项目值;

Trans_ID    Name    Value    Total_Item  
100          I1     0.33333333        3  
100          I2     0.33333333        3  
400          I1     0.33333333        3  
400          I2     0.33333333        3  
800          I1     0.25              4  
800          I2     0.25              4    
900          I1     0.33333333        3  
900          I2     0.33333333        3    
1000         I1     0.2               5  
1000         I2     0.2               5  

请注意,我只显示包含两个项目的交易。

或者可能是3项组合 I1,I2和I3

800          I1     0.25              4  
800          I2     0.25              4  
800          I3     0.25              4   
900          I1     0.33333333        3  
900          I2     0.33333333        3  
900          I3     0.33333333        3  

我如何在sql中编码? 我正在使用msaccess)

2 个答案:

答案 0 :(得分:1)

这些将在Access中使用:

SELECT * FROM Transaction t 
WHERE t.Trans_ID IN 
    (SELECT t1.Trans_ID
     FROM (SELECT *
           FROM Transaction 
           WHERE [Name]="I1")  AS t1 
     INNER JOIN 
          (SELECT *
           FROM Transaction 
           WHERE [Name]="I2")  AS t2 
     ON t1.Trans_ID = t2.Trans_ID)

AND t.Name IN ("I1","I2")


SELECT * FROM Transaction t 
WHERE t.Trans_ID IN 
    (SELECT t1.Trans_ID
     FROM ((SELECT *
           FROM Transaction 
           WHERE [Name]="I1")  AS t1 
     INNER JOIN 
          (SELECT *
           FROM Transaction 
           WHERE [Name]="I2")  AS t2 
     ON t1.Trans_ID = t2.Trans_ID)
     INNER JOIN 
          (SELECT *
           FROM Transaction 
           WHERE [Name]="I3")  AS t3 
     ON t1.Trans_ID = t3.Trans_ID )

AND t.Name IN ("I1","I2","I3")

答案 1 :(得分:0)

对于两项组合,搜索匹配的行并确保在having子句中满足所有条件:

select  Trans_ID
from    `Transaction`
where   Name in ('I1','I2')
group by
        Trans_ID
having  COUNT(distinct Name) = 2

对于三项组合,请检查三项而不是两项匹配:

select  Trans_ID
from    `Transaction`
where   Name in ('I1','I2','I3')
group by
        Trans_ID
having  COUNT(distinct Name) = 3

如果您对所有列感兴趣,请将该查询放在子查询中:

select  *
from    `Transaction`
where   Trans_ID in
        (
        select  Trans_ID
        from    `Transaction`
        where   Name in ('I1','I2','I3')
        group by
                Trans_ID
        having  COUNT(distinct Name) = 3
        )