Home  >  Article  >  Database  >  MySQL 写SQL吃饭中被call起,帮她解决一个SQL的过程_MySQL

MySQL 写SQL吃饭中被call起,帮她解决一个SQL的过程_MySQL

WBOY
WBOYOriginal
2016-06-01 13:26:561266browse

bitsCN.com

正吃饭中被大学同学call起,帮他同事解决一个复杂的SQL问题

 

1 SHE第一封email:

select distinct Test.Name,Test.ID,Plan_Device.ID from Test,Plan_Device where Test.ID in(select TestID from TestResult where (StatusID=1 || StatusID=2)

and PlanResultID=(select ID from PlanResultwhere Sessionid='tangzm_ASBSH2_20131204_10371' and PlanID=(select ID fromTestPlan where Name='MEG_DS3_RRA_FT_MCM3_003')))

and Test.TestPlanID=Plan_Device.TestPlanID

 

select Test.Name,Test.IDfrom Test,AssociateTest where (AssociateTest.TestID=69721or AssociateTest.AssoccaseID=69721) and

(Test.ID=AssociateTest.AssoccaseID orTest.ID=AssociateTest.TestID);

 

现在有这样两个查询,我需要的字段是第二句中的Test.Name,Test.ID和第一句中的Plan_Device.ID

第二句中的69721应该是第一句中查询结果的Test.ID

请问这个语句应该怎么写呢?

 

 

2 ME第二封email:

SELECT DISTINCTTest.Name,Test.ID,Plan_Device.ID

FROMTest,Plan_Device,AssociateTestWHERE Test.ID IN (SELECT TestID FROM TestResult WHERE (StatusID=1 ||StatusID=2)

AND PlanResultID=(

SELECT ID FROM PlanResult WHERE Sessionid='tangzm_ASBSH2_20131204_10371' ANDPlanID=(SELECT ID FROM TestPlan WHERE NAME='MEG_DS3_RRA_FT_MCM3_003')))

ANDTest.TestPlanID=Plan_Device.TestPlanID

and (AssociateTest.TestID=Test.ID ORAssociateTest.AssoccaseID=Test.ID)

AND (Test.ID=AssociateTest.AssoccaseID ORTest.ID=AssociateTest.TestID);

 

黄色部分是我添加进去的,你看看是否可以运行得到你要的结果。

[备注]:我以为是简单的取值,直接把第三张表关联起来把判断条件加上不就行了吗?

 

3 SHE第三封email:

结果显示和第一句查询的结果相同

 

select Test.Name,Test.IDfrom Test,AssociateTest where (AssociateTest.TestID=69721or AssociateTest.AssoccaseID=69721) and

(Test.ID=AssociateTest.AssoccaseID orTest.ID=AssociateTest.TestID);

应该显示两行结果,一行是AssociateTest.TestID=69721查询出来的,另一行是AssociateTest.AssoccaseID=69721查询出来的

 

[备注] 看到她的回复,再仔细看看她第一封email的描述,我知道了她的意思,不仅仅是简单关联,是要把2个结果集归纳到一起,再取出值,换思路吧。

 

 

4 ME第四封email:

想了想,要使用单独集合了,写了sql,发email给她,你试下下面这个:

 

SELECTTest.Name,Test.ID,TP.Plan_Device_IDFROM Test,AssociateTest,(

SELECTDISTINCT Test.Name,Test.ID,Plan_Device.ID Plan_Device_ID

FROM Test,Plan_DeviceWHERE Test.ID IN (SELECT TestID FROM TestResult WHERE (StatusID=1 ||StatusID=2)

ANDPlanResultID=(

SELECT ID FROM PlanResult WHERE Sessionid='tangzm_ASBSH2_20131204_10371' ANDPlanID=(SELECT ID FROM TestPlan WHERE NAME='MEG_DS3_RRA_FT_MCM3_003')))

ANDTest.TestPlanID=Plan_Device.TestPlanID

)TP

WHERE(AssociateTest.TestID=TP.IDOR AssociateTest.AssoccaseID=TP.ID)AND (Test.ID=AssociateTest.AssoccaseID OR Test.ID=AssociateTest.TestID);

 

 

5SHE第五封email:

 

这个可以用, 谢谢!

 

OK,搞定了。

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn