您好,欢迎访问一九零五行业门户网

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

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
其它类似信息

推荐信息