返回一定数量的行满足特定条件的值的查询

时间:2013-12-09 16:25:16

标签: mysql vb.net rdlc

我正在尝试制作一份报告,而且我正在设计一些疑问,以显示我需要的数据。

我有两张桌子:

+-----------+------------+----------+
+TherapyID  + CostumerID + ClinicID +
+-----------+------------+----------+
+    1      +  John      + Clinic 1 +
+-----------+------------+----------+
+    2      +  Susan     + Clinic 2 +
+-----------+------------+----------+
+    3      +  Mary      + Clinic 3 +
+-----------+------------+----------+


+-----------+--------------+-----------+--------+
+TherapyID  + TherapyLine  + Treatment + Result +
+-----------+--------------+-----------+--------+
+     1     +       1      +     A     + Success+
+-----------+--------------+-----------+--------+
+     1     +       2      +     B     + Success+
+-----------+--------------+-----------+--------+
+     1     +       3      +     C     + Success+
+-----------+--------------+-----------+--------+
+     2     +       1      +     A     + Success+
+-----------+--------------+-----------+--------+
+     2     +       2      +     B     + Fail   +
+-----------+--------------+-----------+--------+
+     2     +       3      +     C     + Success+
+-----------+--------------+-----------+--------+
+     3     +       1      +     A     + Success+
+-----------+--------------+-----------+--------+
+     3     +       2      +     B     + Success+
+-----------+--------------+-----------+--------+
+     3     +       3      +     C     + Fail   +
+-----------+--------------+-----------+--------+

我需要进行查询,向我显示已成功接受所有治疗的客户或疗法 A,B,C

查询结果应如下所示:

+------------+-------------+----------+---------+-----------+---------+
+ TherapyID  + TherapyLine + Customer +  Clinic + Treatment + Result  +
+------------+-------------+----------+---------+-----------+---------+
+     1      +     1       +  John    + Clinic 1+    A      + Success + 
+------------+-------------+----------+---------+-----------+---------+
+     1      +     2       +  John    + Clinic 1+    B      + Success +
+------------+-------------+----------+---------+-----------+---------+
+     1      +     3       +  John    + Clinic 1+    C      + Success +
+------------+-------------+----------+---------+-----------+---------+

这是唯一的治疗方法,所有治疗 A,B,C 成功 我真的不知道如何查询这个,我到现在为止做了什么 allways返回 TherapyID * 2,3 *的结果,结果成功。 请事先提供帮助。

2 个答案:

答案 0 :(得分:0)

select t1.TherapyID,TherapyLine,t1.CostumerID as Customer,Clinic,Treatment, 
Result from table1 t1,table2 t2 where t1.TherapyID =t2.TherapyID and
t2.result='Success' group by customerID having count(customeID)=3

答案 1 :(得分:0)

SELECT t1.TherapyID,TherapyLine,CustomerID as Customer,ClinicID as Clinic,Treatment,Result  
FROM FROM t1 JOIN t2 ON t1.TherapyID=t2.TherapyID WHERE CustomerID IN 
(SELECT CustomerID
FROM t1 JOIN t2 ON t1.TherapyID=t2.TherapyID AND Result='Success' 
GROUP BY CustomerID 
HAVING COUNT(CustomerID)=3)