Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to combine more than one query in same table

I have the following table:

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)
)

With the following data:

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')

I want to query this data to get the result set of all the students who do not have a grade of "ra" in any of the subjects.

like image 247
Saravanan Avatar asked Dec 29 '12 06:12

Saravanan


People also ask

How do I merge two MySQL queries?

The MySQL UNION operator is used to combine the result sets of 2 or more SELECT statements. It removes duplicate rows between the various SELECT statements. Each SELECT statement within the UNION operator must have the same number of fields in the result sets with similar data types.


1 Answers

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
like image 88
John Woo Avatar answered Oct 17 '22 03:10

John Woo