gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 13:02:12 24 4
gpt4 key购买 nike

我有下表:

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”成绩的所有学生的结果集。

最佳答案

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
  • 关于sql - 如何在同一个表中组合多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14079388/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com