4

我在 SO 上搜索了它,但我无法得到正确的答案。

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 分的学生信息?

4

6 回答 6

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
于 2013-07-03T07:42:42.090 回答
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 ..

于 2013-07-03T06:33:43.343 回答
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 )
于 2013-06-19T06:03:07.713 回答
0

你的 Marks 表可以先标准化!

Marks
-----------------
rollno       int
Subject_ID   int --( FK )
Subject_Val  int

Subjects
-----------------
ID             int
Subject_Name   varcahar(20)

在你做这个小改变之后,事情会变得更加清晰

于 2013-07-03T07:09:42.513 回答
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)
于 2013-06-19T06:34:17.987 回答
-1

我也是编码新手,但你可以尝试这样的事情,但在此之前总是让问题更清楚

SELECT student.* from student InnerJoin marks ON studnet.roll_no=marks.student) where (sub1>=35 && sub2>=35 and so on)
于 2013-06-19T05:48:32.647 回答