MySQL Query (advanced) (each punctuation is the focus)

Source: Internet
Author: User
Tags mysql query tidy

MySQL is a common need in work, so it has to be mastered, and what we have been saying is how to save it.

You'll only be able to save a fart. So I hope you can read the data in the way of how to do a little bit more effort.

This and the last one is dry, and I am also the first time to learn. Anyway, I understand that, so I read it over and over again, it is slowly a little feeling.

At present also tidy very disorderly, first will look, and so on later thought up I will further tidy up.

Simple query: Single-table query and multi-table query
Distinct: Go heavy

Single-table only query with distinct

GROUP BY: Grouping

Single query for multiple tables with GROUP by

SELECT * from Depariment,employee;

This is called the Cartesian product.

To stitch all the records together again.

Query two tables. Put two tables in a table, and then line them up.

All the records of the two tables are spliced once (if you don't understand it, you can actually look at them).

This approach is meaningless.

SELECT * from employee,department where employee.dep_id = department.id; The job is to watch the work.

This is the same part of extracting two tables. But the different parts aren't left, right?

It's going to be a magical thing to use.

INNER JOIN : Link to a virtual table with the same portion of two tables as on condition.

SELECT * from the employee inner join Department on employee.dep_id = department.id;

Translation: I check employee first, on this basis continue to check department , on the following is the condition.

SELECT * FROM Employee left JOIN Department on employee.dep_id = department.id;

The top is the left link.

Translation: The records of the left table are retained on the basis of the on condition, taking the two tables together.

SELECT * FROM Employee Right Join department on employee.dep_id = department.id;

Translation: Keep Right table

Above is the right link below said is, to the left and the side of the table are not the same all take out, called: Full link Union

SELECT * FROM Employee left JOIN Department on employee.dep_id = department.id

Union

SELECT * FROM Employee Right Join department on employee.dep_id = department.id;

Sub-query:

In is because there may be multiple

The number of enquiries is only 1 People's Department name:

Select name from department where ID in (select dep_id from Employee GROUP by DEP_ID have count (id) = 1);

MySQL Query (advanced) (each punctuation is the focus)

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.