Logical implication conditions of MySql Data Query

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.