sql – 如何在同一个表中组合多个查询

前端之家收集整理的这篇文章主要介绍了sql – 如何在同一个表中组合多个查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有下表:
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,2,'raja','e','selvam','i')

我想查询这些数据,以获得所有在任何科目中没有“ra”成绩的学生的结果集.

解决方法

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

> SQLFiddle Demo Link

原文链接:https://www.f2er.com/mssql/77783.html

猜你在找的MsSQL相关文章