我搜索了它,但我无法得到正确答案。
Student
------------------
rollno int PK
name varchar(20)
class varchar(20)
另一张表是
Marks
-----------------
rollno FK
sub1 int
sub2 int
sub3 int
sub4 int
sub5 int
sub1
,sub2
等包含主题标记。现在我想要一个查询,它将显示在超过2个科目中有> 35分的学生信息?
答案 0 :(得分:2)
select rollno,
case when sub1 < 35 then 0 else 1 end +
case when sub2 < 35 then 0 else 1 end +
case when sub3 < 35 then 0 else 1 end +
case when sub4 < 35 then 0 else 1 end +
case when sub5 < 35 then 0 else 1 end +
end
as [Count]
from student,marks where count > 2
答案 1 :(得分:1)
您可以使用CASE WHEN
并对超过35的字段求和。
SELECT s.rollno, s.name, s.class
FROM Student s join Marks m on (s.rollno = m.rollno)
where (CASE WHEN sub1>35 THEN 1 ELSE 0 END +
CASE WHEN sub2>35 THEN 1 ELSE 0 END +
CASE WHEN sub3>35 THEN 1 ELSE 0 END +
CASE WHEN sub4>35 THEN 1 ELSE 0 END +
CASE WHEN sub5>35 THEN 1 ELSE 0 END) > 2;
您可以在此处SQL Fiddle..
查看答案 2 :(得分:0)
DECLARE @tempRollnoTable(rno int)
INSERT INTO @tempRollnoTable
SELECT rollno FROM marks
WHERE sub1 > 35
UNION ALL
SELECT rollno FROM marks
WHERE sub2 > 35
UNION ALL
SELECT rollno FROM marks
WHERE sub3 > 35
UNION ALL
SELECT rollno FROM marks
WHERE sub4 > 35
UNION ALL
SELECT rollno FROM marks
WHERE sub5 > 35
SELECT * FROM student
WHERE rollno IN ( SELECT rno FROM @tempRollnoTable
GROUP BY rno HAVING COUNT(*) > 2 )
答案 3 :(得分:0)
试试这个
select student.* from student inner join marks on student.rollno= marks.rollno
where marks.rollno in(
Select a.* from (
Select RollNo from marks where sub1>35
Union ALL
Select RollNo from marks where sub2>35
Union ALL
Select RollNo from marks where sub3>35
Union ALL
Select RollNo from marks where sub4>35
Union ALL
Select RollNo from marks where sub5>35) a
having(rollno)>1)
答案 4 :(得分:0)
你的Marks表可以先规范化了!
Marks
-----------------
rollno int
Subject_ID int --( FK )
Subject_Val int
Subjects
-----------------
ID int
Subject_Name varcahar(20)
在你做了这个小小的改变后,事情会变得更加清晰
答案 5 :(得分:-1)
我也是编码的新手,但是你可以尝试这样的事情,但在总是让问题更清楚之前
SELECT student.* from student InnerJoin marks ON studnet.roll_no=marks.student) where (sub1>=35 && sub2>=35 and so on)