仅当行存在在另一个表中时,SQL显示才会结果

发布于 2025-02-11 13:53:17 字数 2350 浏览 0 评论 0原文

如果存在该特定值,则有没有办法显示结果,而另一个表中有多个条目?

例如,我仅在类型是数学时才尝试显示老师的名字。如果没有数学的条目,则专栏老师应该不会显示。

用户名u发送电子邮件
给我[email  procected]
-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ceee3fbccbda2efe3e1">[email protected]
Classes CUsername
FirstMe
SecondMe
ThirdMe
ThirdWiz
ClassesTeacher TType
FirstAMath
Secondb数学
第三cnull

最终结果,因为我存在数学课,

用户名电子邮件老师
]a
me  prectioned]

[ email 对于Wiz的课程,没有数学类

email email用户
wiz wiz我遇到的问题都不

是我的查询即使用户没有数学,但有其他类别,或者它显示数学,也没有同时显示数学,即使我的查询也没有条目。

我当前的查询如下:

from User u
join classes c on c.username=u.username
join teachers t on t.classes=c.classes and type ='Math' <---- If I left join, I get 2 entries, if I join, I get 0 entries for non-math folks
where username ='Me' 

因此,我希望查询的3个方案涵盖:

  1. 如果用户表中不存在用户(无访问)

  2. 如果用户存在但没有任何数学(显示用户无条目)

  3. 如果用户存在并且具有数学(无论其他类型如何)显示类 - 那里的最终情况

我会在案例语句中做某种选择吗?此查询是更大的执行查询的一部分。

这是SQL Server Management Studio

Is there a way to display results only if that particular value exists, if there are multiple entries of it in another table?

For example, I am trying to display teacher name only if the type is Math. If there are no entries of Math, the column Teacher should display none.

UserName UEmail
Me[email protected]
Wiz[email protected]
Classes CUsername
FirstMe
SecondMe
ThirdMe
ThirdWiz
ClassesTeacher TType
FirstAMath
SecondBMath
ThirdCNULL

Final result as Math Classes exist for Me

UserNameEmailTeacher
Me[email protected]A
Me[email protected]B

Final result as Math Classes exist for Wiz with no math classes

UserNameEmailTeacher
Wiz[email protected]None

The issue I'm running into is that my query is displaying either no entries even though user has no math but has other classes, or it's displaying Math and None at the same time.

My current query is as follows:

from User u
join classes c on c.username=u.username
join teachers t on t.classes=c.classes and type ='Math' <---- If I left join, I get 2 entries, if I join, I get 0 entries for non-math folks
where username ='Me' 

So the 3 scenarios I want the query to cover:

  1. If user does not exist in the user table (no entry)

  2. If user exists but doesn't have any math (show user None entry)

  3. If user exists and has math (regardless of any other type) display classes - final situation up there

Do I do some kind of select count in the case statement? This query is part of a bigger execution query.

This was SQL Server Management Studio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2025-02-18 13:53:17

这样的事情会解决您的问题吗?

select u.Username, 
       Email, 
       CASE WHEN types = 'MATH'
       THEN teacher
       ELSE 'None'
       end as teacher
from users u
join class c on c.username=u.username
join teacher t on t.classes=c.classes
WHERE u.Username = 'Wiz'

Would something like this solve your question?

select u.Username, 
       Email, 
       CASE WHEN types = 'MATH'
       THEN teacher
       ELSE 'None'
       end as teacher
from users u
join class c on c.username=u.username
join teacher t on t.classes=c.classes
WHERE u.Username = 'Wiz'

db fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文