如何优化SQL查询? [英] How to optimize SQL query ?

查看:54
本文介绍了如何优化SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3张如下表格

I have 3 tables like below

Student 
   	stdId
	stdName
        stdCourseID
StudentCources
        stdCourseID
        courseName
        stdeventID
StudentEvents
        stdeventID
        eventName



i想要填写所有这些属性,如下所示


i want to fill all these properties like below

stdId
stdName

stdCourseName

stdEventName



当我尝试使用此查询时,我将获得重复的stdId,stdName和stdCourseID,以及重新生成项目列表中的corseName和eventName


when i try with this query i am getting duplicate stdId , stdName and stdCourseID follewd by list of remaning items corseName and eventName

select * from Student as s WITH(NOLOCK) 
join StudentCources as se WITH(NOLOCK) on sc.stdCourseID = s.stdCourseID 
join StudentEvents as se WITH(NOLOCK) on se.stdeventID = se.stdeventID  
where s.stdId=118





我尝试过:





What I have tried:

select name,courseName,eventName from Student as s WITH(NOLOCK) 
join StudentCources as se WITH(NOLOCK) on sc.stdCourseID = s.stdCourseID 
join StudentEvents as se WITH(NOLOCK) on se.stdeventID = se.stdeventID  
where s.stdId=118



你能告诉我如何优化这个应该获取所有数据的查询


Could you please tell me how to optimize this query that should fetch all data

推荐答案

另外,如果你的学生没有在StudentCources[sic]表和StudentEvents表中有一条记录,它将返回一个空集,因为你使用的是隐含的INNER JOIN。
Also, if your student doesn't have a record in either the "StudentCources" [sic] table and "StudentEvents" table, it will return an empty set because you are using an implied INNER JOIN.


这篇关于如何优化SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆