7

我有下表:

CREATE TABLE studenttest 
(
    YEAR INT,
    DEPT VARCHAR(5),
    SEM INT,
    REGNO INT,
    NAME VARCHAR(20),
    ENGLISH VARCHAR(2),
    MATHS VARCHAR(2),
    PHYSICS VARCHAR(2),
    CHEMISTRY VARCHAR(2),
    EG VARCHAR(2),
    FOC VARCHAR(2),
    LAB1 VARCHAR(2),
    LAB2 VARCHAR(2),
    LAB3 VARCHAR(2)
)

使用以下数据:

INSERT INTO studenttest 
 values
 (2010,'cse',3,1,'saravaanan','a','b','c','d','ra','f','g','h','i'),   
 (2010,'cse',3,2,'raja','ra','b','c','d','e','f','g','h','i'),
 (2010,'cse',3,3,'selvam','a','b','c','d','e','f','g','h','i')

我想查询这些数据以获取所有科目中没有“ra”成绩的学生的结果

4

6 回答 6

5
SELECT  a.*
FROM    studenttest a
        LEFT JOIN
        (
          SELECT regno, name
          FROM studenttest
          WHERE ENGLISH = 'ra' OR
                MATHS = 'ra' OR
                PHYSICS = 'ra' OR
                CHEMISTRY    = 'ra' OR
                EG  = 'ra' OR   
                FOC = 'ra' OR   
                LAB1 = 'ra' OR  
                LAB2 = 'ra' OR  
                LAB3 = 'ra'
        ) b ON a.regno = b.regno 
               AND a.Name = b.Name   -- this line is OPTIONAL
WHERE   b.regno IS NULL
于 2012-12-29T06:51:13.807 回答
4

只是@Mahmoud's version with the UNPIVOTand the的替代方案CTE,这也可以使用 a 来完成UNION ALL

SELECT Name
FROM
(
  SELECT Name, ENGLISH grade, 'English' subject
  from studenttest
  union all
  SELECT Name, MATHS grade, 'MATHS' subject
  from studenttest
  union all
  SELECT Name, PHYSICS grade, 'PHYSICS' subject
  from studenttest
  union all
  SELECT Name, CHEMISTRY grade, 'CHEMISTRY' subject
  from studenttest
  union all
  SELECT Name, EG grade, 'EG' subject
  from studenttest
  union all
  SELECT Name, FOC grade, 'FOC' subject
  from studenttest
  union all
  SELECT Name, LAB1 grade, 'LAB1' subject
  from studenttest
  union all
  SELECT Name, LAB2 grade, 'LAB2' subject
  from studenttest
  union all
  SELECT Name, LAB3 grade, 'LAB3' subject
  from studenttest
) un
where grade <> 'ra'
group by name
having count(grade) = 9

执行与更少代码UNPIVOT相同的步骤。UNION ALL当您的数据不像您的表那样规范化时,这些查询非常有用。

然后,如果您想要所有数据(不仅仅是名称),您可以再次将上述查询重新加入表:

select *
from studenttest t
inner join
(
  SELECT Name
  FROM
  (
    SELECT Name, ENGLISH grade, 'English' subject
    from studenttest
    union all
    SELECT Name, MATHS grade, 'MATHS' subject
    from studenttest
    union all
    SELECT Name, PHYSICS grade, 'PHYSICS' subject
    from studenttest
    union all
    SELECT Name, CHEMISTRY grade, 'CHEMISTRY' subject
    from studenttest
    union all
    SELECT Name, EG grade, 'EG' subject
    from studenttest
    union all
    SELECT Name, FOC grade, 'FOC' subject
    from studenttest
    union all
    SELECT Name, LAB1 grade, 'LAB1' subject
    from studenttest
    union all
    SELECT Name, LAB2 grade, 'LAB2' subject
    from studenttest
    union all
    SELECT Name, LAB3 grade, 'LAB3' subject
    from studenttest
  ) un
  where grade <> 'ra'
  group by name
  having count(grade) = 9
) t2
  on t.name = t2.name

请参阅带有演示的 SQL Fiddle

于 2012-12-29T13:44:31.990 回答
3

你可以这样做:

WITH AllSubjectsGrades
AS
(
  SELECT Name, Grade
  FROM
  (
    SELECT 
      Name, 
      ENGLISH, MATHS, PHYSICS,  CHEMISTRY,
      EG,  FOC, LAB1, LAB2, LAB3
    FROM studenttest
  ) t
  UNPIVOT
  (
    grade
    FOR Subject IN(ENGLISH, MATHS, PHYSICS, CHEMISTRY,
                   EG,  FOC, LAB1, LAB2, LAB3)
  ) u
)
SELECT Name
FROM AllSubjectsGrades
WHERE GRADE <> 'ra'
GROUP BY Name
HAVING COUNT(GRADE) = 9;

SQL 小提琴演示

这只会给你 student selvam,因为在英语中有 'ra saravaanan'EGraja有 'ra'。但selvam从来没有上ra过任何科目。就是这样

GROUP BY Name
HAVING COUNT(GRADE) = 9;

做。

于 2012-12-29T06:52:45.743 回答
2

另一种方法:

SELECT * 
FROM studenttest
WHERE 'ra' NOT IN 
      (ENGLISH, MATHS, PHYSICS, CHEMISTRY, EG, FOC, LAB1, LAB2, LAB3) ;
于 2012-12-29T13:50:44.187 回答
2

你是这个意思吗?

SELECT * 
FROM studenttest
WHERE 
    ENGLISH <> 'ra' AND
    MATHS <> 'ra' AND
    PHYSICS <> 'ra' AND
    CHEMISTRY    <> 'ra' AND
    EG  <> 'ra' AND   
    FOC <> 'ra' AND   
    LAB1 <> 'ra' AND  
    LAB2 <> 'ra' AND
    LAB3 <> 'ra'
于 2012-12-29T06:58:37.650 回答
1
SELECT *
FROM studenttest
WHERE ENGLISH + ',' + MATHS + ',' + PHYSICS + ',' + CHEMISTRY + ',' + EG + ',' + FOC + ',' + LAB1 + ',' + LAB2 + ',' + LAB3 NOT LIKE '%ra%'

SQLFiddle上的演示

于 2012-12-29T09:57:41.857 回答