MySQL internal connection, external connection, self-connection

Source: Internet
Author: User
Tags one table

A) Internal connection (equivalent connection): Query customer Name, order number, order price
---------------------------------------------------
Select C.name,o.isbn,o.price
From customers c INNER JOIN orders O
where c.id = o.customers_id;
---------------------------------------------------
Select C.name,o.isbn,o.price
From customers c join orders O
where c.id = o.customers_id;
---------------------------------------------------
Select C.name,o.isbn,o.price
From Customers c,orders O
where c.id = o.customers_id;
---------------------------------------------------
Select C.name,o.isbn,o.price
From customers c join orders O
on c.id = o.customers_id;
---------------------------------------------------
Note: The internal connection (equivalent connection) can only query out multiple tables, join the same field records


Second) outside connection: According to the customer group, query each customer's name and order number
---------------------------------------------------
Left Outer connection:
Select C.name,count (O.ISBN)
From customers C left outer JOIN orders O
On c.id = o.customers_id
Group BY C.name;
---------------------------------------------------
Right outer connection:
Select C.name,count (O.ISBN)
From Orders O right outer join customers C
On c.id = o.customers_id
Group BY C.name;
---------------------------------------------------
Note: Outer joins can query more than one table, join fields of the same record, and according to a party, the other party does not conform to the same record forcibly queried


III) Self-connection: Find out the boss of AA is EE
---------------------------------------------------
Internal Self-Connection:
Select Users.ename,boss.ename
From Emps users INNER join Emps boss
on users.mgr = Boss.empno;
---------------------------------------------------
External self-Connection:
Select Users.ename,boss.ename
From Emps users left OUTER join Emps boss
on users.mgr = Boss.empno;
---------------------------------------------------
Note: A self-join is a table that, by way of an alias, is treated as multiple tables and then connected.
At this point the connection can be connected, but also can be used outside the connection

MySQL internal connection, external connection, self-connection

Related Article

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.