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.