Advanced SQL subqueries

A: the preparation of sub-queries:

  Queries students, "Zhang Han" class number, and then check out the same, "Zhang Han" class number of students in the student table number, name, and number of classes

  1) select ClassID from studentinfo where studentname = "Zhang Han"; // Query students, "Zhang Han" class number

          select id, name, ClassID from studentinfo where ClassID = (select ClassID from studentinfo where studentname = "Zhang Han"); // query the same table with the students of classes a student ID

       2)select a.studentname from studentinfo a

   inner join exam b

   on a.id=b.studentid

   inner join subject c

   on c.id=b.subjectid

   where c.subjectname = "C-based software programming language understanding" and b.score = 90;

Two: Use subqueries in UPDATE, DELETE, INSERT statement

  1)UPDATE

      UPDATE score = 55 // All set exam results revised to 55

      where studentid = (// modify the condition score of 55

      select id from studentinfo where studentname="章涵"

      and subjectid=(

      select id from subject where subjectname = "based on the C programming language understanding software"

      ))

   2)DELETE

      DELETE from exam where studentid=(

      select id from studentinfo where studentname="章涵");

    3)INSERT

      INSERT table names select field list from table

Three: Advanced Subqueries

  1)IN/NOT IN 

    select a.studentname from studentinfo where id IN

    (Select studentid from exam where score = 90 // query score of 90 student numbers

    and subjectid=

    (Select subjectid from subject where subjectname = "based on the C programming language understanding software")) // query "based on the C programming language understanding software" Course Code

    select a.studentname from studentinfo where id NOT IN

    (select studentid from exam where score=90

    and subjectid= 

    (Select subjectid from subject where subjectname = "based on the C programming language understanding software")) // query did not participate, "based on the C programming language understanding software"

  2)EXISTS/NOT EXISTS

    select studentid, exam from exam where subjectid = 2 and exists (select studentid from exam where exam <60); // first check failing grade of id, if returned at least one row, a query is performed results in Table id subjectid = 2 and performance information

    select studentid, exam from exam where subjectid = 2 and not exists (select studentid from exam where exam <60); // first check failing grade of id, if no return value, the query

      Results Table subjectid = id and grade information 2

  3)ALL、ANY、SOME

    select * from exam where score> ALL (select score from exam where subjectid = 1) // query subjects numbered 1 of this course all students' test scores are a big information

    select * from exam where score> ANY (select score from exam where subjectid = 1) // query the account number is any one of a large student test scores have information

    select * from exam where score> SOME (select score from exam where subjectid = 1) // query the account number is any one of a large student test scores have information

 

      

Guess you like

Origin www.cnblogs.com/W19990605/p/11568582.html