MYSQL Talk about Join Association query usage

Source: Internet
Author: User

Now let's look at the SQL Association lookup join,

This is a big problem in my early days, how two tables relate to each other, in fact, there's something called join, and then there's the inner join,right Join,left join

I basically use left JOIN, then right join people know =a= other I have no use

Different references:

JOIN: Returns a row if there is at least one match in the table
Left JOIN: Returns all rows from the left-hand table even if there is no match in the right table
Right JOIN: Returns all rows from the right-hand table even if there is no match in the left table
Full JOIN: Returns rows whenever there is a match in one of the tables

Here, give me a m-show chestnut:

The code is as follows Copy Code
SELECT Show_person.uid, Show_person.nickname, show_category.category,show_category.id from Show_person left JOIN Show_ Category on Show_person.category=show_category.id WHERE show_category.id= $category ORDER by Show_person.uid

To establish the association between person and category, the primary table (the self coined = =) is person, so the person is fully output, even if there is no category definition.

The Select section uses tables. fields, if a table is all selected, the field is selected with *, but is said to be less efficient


Now it's time to learn something new.

First or this code (and the reason is because of the previous article):

The code is as follows Copy Code
SELECT Pr_rates.pid,count (PR_RATES.IP), pr_works.title,pr_works.realname,pr_college.name from Pr_works right JOIN pr_ Rates on Pr_rates.pid=pr_works.pid left JOIN Pr_college on pr_works.college=pr_college.id GROUP by PID ORDER by COUNT (Pr_r ATES.IP) DESC

Why is there a left join right join, the equivalent of a person standing in the middle, holding hands, two of people naturally do not feel, in fact, a person in the middle of the link, his left hand connected to a person's right hand, the right hand connected to a person's left hand, established an association, So we choose a table that has a common item with two tables as the middleman, which is pretty much what it means.

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.