Usage of join, left join, and right join in mysql !!!

Source: Internet
Author: User

When I checked the brophp framework today, I conveniently reviewed the combined query in mysql. (In the past, I would use the simplest "select * from..."). Others do not dare to use them as much as they do. I am cleaning out the "blind spots" in the past.
Next, let's talk about the simple joint query. Of course, we have to create several database tables first. I'm limited. I will use two tables for demonstration at the moment:
Create a bro_cats table:
 
Create table bro_cats (
Id int (11) not null auto_increment,
Name varchar (128) not null default '',
Desn varchar (128) not null default '',
Primary key ()
);
Create a bro_articles table:
 
Create table bro_articles (
Id int not null auto_increment,
Cid int not null,
Name varchar (128) not null,
Content test not null,
Primary key (id)
);
Next, we will insert data into it.
 
Insert into bro_cats (name, desn) values ('php', 'php demo ');
 
Insert into bro_cats (name, desn) values ('jsp ', 'jsp demo ');
 
Insert into bro_cats (name, desn) values ('asp ', 'asp demo ');
Insert into bro_articles (cid, name, content) // cid = 1 in the php class
 
Values (1, 'This article of php1', 'php content1 ');
 
Insert into bro_articles (cid, name, content) // cid = 1 in the php class
 
Values (1, 'This article of php2 ', 'php content2 ');
 
Insert into bro_articles (cid, name, content) // cid = 2 in the jsp class
 
Values (2, 'This article of jsp ', 'jsp content ');
 
Insert into bro_articles (cid, name, content) // cid = 3 in asp class
 
Values (3, 'This article of asp1', 'asp content1 ');
 
Insert into bro_articles (cid, name, content) // cid = 3 in asp class
 
Values (3, 'This article of asp2 ', 'asp content2 ');
Next, we will test it using the join statement.
 
Left join: left join
Select bro_cats.name as catsname, bro_cats.desn as description, bro_articles.name as articlesname, bro_articles.content as articlecontent from bro_cats left join bro_articles on bro_cats.id = bro_articles.cid;
Right join: right join
Select bro_cats.name as catsname, bro_cats.desn as description, bro_articles.name as articlesname, bro_articles.content as articlecontent from bro_cats right join bro_articles on bro_cats.id = bro_articles.cid;
Join: join
Select bro_cats.name as catsname, bro_cats.desn as description, bro_articles.name as articlesname, bro_articles.content as articlecontent from bro_cats join bro_articles on bro_cats.id = bro_articles.cid;
What are the specific effects of these three items? I suggest you try and compare them to draw a conclusion.
In the future, multiple table queries will be extended, and how to optimize multi-table queries. Welcome to my blog !! I remember reading the reply amount. I hope you will correct me if there is something wrong. After all, it's a cainiao and you have to learn from the old birds !!!
Author "You_Can"

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.