本篇内容介绍了“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的用法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。