Mysql self-connection learning notes

Source: Internet
Author: User

Mysql self-join queries some data first Based on the condition and then returns the returned value to query some data. This is mysql self-join. Of course, left join can also be used, next let's take a look at the usage of mysql self-connection.

The auto-join processing speed of mysql is much faster than that of subqueries. Therefore, it is necessary to learn self-join.

Common SQL subquery statements

The Code is as follows: Copy code

SELECT 'id', 'bic 'FROM 'biao' WHERE 'id' = (SELECT 'id' FROM 'biao' WHERE 'id' = '9696e ');

Mysql self-join SQL statement

The Code is as follows: Copy code

SELECT 'id', 'bic 'FROM 'biao' AS a, 'biao' AS B WHERE. id = B. id AND B. id = 'www. bKjia. c0m ';

Self-join SQL statements look refreshing and elegant.

From the two examples, we can see that the self-join syntax structure is very simple, but the semantic result is often not
So easy to understand. The two tables listed here can solve many practical problems if they are used properly,
For example, how to transfer between any two sites.

The Code is as follows: Copy code

SELECT R1.company, R1.num
FROM route R1, route R2, stops S1, stops S2
WHERE R1.num = R2.num AND R1.company = R2.company
AND R1.stop = S1.id AND R2.stop = S2.id
AND S1.name = 'craiglockhart'
AND S2.name = 'tollcross'

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.