mysql中的join、left join和right join的用法
本篇内容介绍了“mysql中的join、left join和right join的用法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
暂时先用两个表来演示:
创建bro_cats表:
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()
);
创建bro_articles表:
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)
);
接下来我们向里边插入数据
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 demo’);
INSERT INTO bro_articles(cid, name, content) //php 类中 cid=1
values(1,’this article of php1’, ‘php content1’);
INSERT INTO bro_articles(cid, name, content) //php 类中 cid=1
values(1,’this article of php2’, ‘php content2’);
INSERT INTO bro_articles(cid, name, content) // 类中 cid=2
values(2,’this article of jsp’, ‘jsp content’);
INSERT INTO bro_articles(cid, name, content) //asp 类中 cid=3
values(3,’this article of asp1’, ‘asp content1’);
INSERT INTO bro_articles(cid, name, content) //asp 类中 cid=3
values(3,’this article of asp2’, ‘asp content2’);
下一步我们就用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
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
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;
具体的这三个是什么效果,我还是建议大家试一下比较一下自己得出结论。
“mysql中的join、left join和right join的用法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341