在两个表中查找值

fhity93d  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(206)

关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。

两年前关门了。
改进这个问题
我有两张table“学生”和“书”
学生有-学号|姓名|年龄
书名-isbn |书名|科目|学号|
我需要在book表中的相关主题列中找到student\u id、student\u name、使用数学和计算机的学生的年龄。顺便说一句,这些表没有联接。有人能给我显示正确的sql查询吗?

q3qa4bjr

q3qa4bjr1#

select  a.student_ID
,       a.student_name
,       a.age
from    student a
join    book b  
on      a.student_ID = b.student_ID 
where   b.subject like '%mathematics%'
        or b.subject like '%computer%'
group by
        a.student_ID
,       a.student_name
,       a.age
having  count(distinct case when b.subject like '%mathematics%' then 1
              when b.subject like '%computer%' then 2 end) = 2
bprjcwpo

bprjcwpo2#

您可以使用表和主题过滤器之间的联接来检查所涉及的行是否包含2个不同的主题

select a.student_ID,a.student_name,a.age
from  student a
inner join book b  on a.student_ID = b.student_ID 
where b.subject in ('mathematics', 'computer')
group by a.student_ID,a.student_name,a.age 
having count(distinct subject) = 2

相关问题