The SQL language does not contain logical operations. However, a logical predicate can be equivalent to p-q ≡ ┐ p ∨ q. we use a specific question for analysis: (for specific tables and data, see the article: EXISTS and NOTEXISTS in Mysql database.) Question: Query all courses selected by at least 200215122 of Students
The SQL language does not contain logical operations. However, a logical predicate can be equivalent to p-q ≡ ┐ p ∨ q. we use a specific question to analyze: (for specific tables and data, see the article: EXISTS and not exists in the Mysql database.) Question: Query all courses selected by at least 200215122 of Students
The SQL language does not contain logical operations. However, a logical predicate can be equivalent to p-> q ≡ ┐ p ∨ q.
We use a specific question for analysis: (for specific tables and data, see the article: EXISTS and not exists in the Mysql database)
Question: query the number of all students who have selected at least 200215122 of the students.
This query can be expressed by logical implication: to query the students whose student ID is x, if 200215122 of the students take the course y for all the courses, x also takes the course y.
The formal representation is as follows:
P represents the predicate "Student 200215122 took course y"; q represents the predicate "Student x took course y ".
The above query is:(? Y) p-> q
The query can be converted to the following equivalent form:
(? Y) p-> qZookeeper (? Y (round (p-> q) round (? Y (sort p sort q) then analyze? Y (p ∧ ┐ q)
The meaning of this statement is as follows: no such course y exists. Student x chooses y, but student x does not.
Corresponding SQL statement:
SELECT DISTINCT SnoFROM SC SCXWHERE NOT EXISTS (SELECT * FROM SC SCY WHERE SCY.Sno='200215122' AND NOT EXISTS (SELECT * FROM SC SCZ WHERE SCZ.Sno=SCX.Sno AND SCZ.Cno=SCY.Cno));
First, the first record (200215121) in SCX and the first record of SCY Sno = '000000' are judged. Data is returned and the value of not exists is false;
Then, judge the next Sno = '20160301' record of SCY until the Sno = '20160301' of all SCY is traversed. The value of not exists Is always false, the WHERE value of the intermediate layer is false, and the not exists value of the outermost layer is true. Therefore, the first data item meets the query conditions and is stored in the result table;
Query the next record in SCX until you traverse all the data in the SCX table.
Result output: