Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the Multi-condition combination query skillfully by SqlServer

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about how SqlServer skillfully solves the multi-condition combination query, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

In the development, we often encounter situations that require a combination of conditions, such as three tables, grade table Grade (GradeId,GradeName), class Class (ClassId,ClassName,GradeId), and student table Student (StuId,StuName,ClassId). Now we can query student information according to grade Id, class Id, student name, these three conditions can be combined arbitrarily.

There is more than one way, here is an ingenious way to use a query to achieve, let's write a process, as follows:

The code is as follows:

Create proc up_select_student_combocondition @ gradeId int, @ classId int, @ stuName nvarchar (10) as select s.classname from student s join class con s.classid=c.classid join grade g on c.gradeid=g.gradeid where (@ gradeId=-1 or g.gradeid=@gradeId) and (@ classId=-1 or c.classid=@classId) and (@ stuName='' or s.stuName=@stuName) go

-- so:

Exec up_select_student_combocondition-1 Id 1Q'--this is to query all the students' information, exec up_select_student_combocondition 2, and this is to query the information of the students whose grade exec up_select_student_combocondition is 2, and this is to query the information of the students whose grade exec up_select_student_combocondition is 2, and this is to query the information of the students in the class with the Id of 4. This is to query the student information of grade Id 2 and class Id 4-etc....

After reading the above, do you have any further understanding of how SqlServer skillfully solves the multi-conditional combination query? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report