十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章主要讲解了如何使用MySQL连接查询、联合查询、子查询,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
创新互联是专业的北海网站建设公司,北海接单;提供网站设计制作、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行北海网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
连接查询:
-- 实验表结构 create table student( id int, name varchar(15), gender varchar(15), cid int ); create table class( cid int, cname varchar(15) ); drop table student,class; -- 实验表数据: insert into student values(1,"lilei","male",1),(2,"hanmeimei","male",2),(3,"jack","male",1),(4,"alice","female",4); --这里特意创建了一个class中没有的4 insert into class values(1,"linux"),(2,"python"),(3,"java"),(5,"html5");--这里特意创建了一个student中没有的5 select * from student; select * from class;
-- 内连接 -- select * from student inner join class; --结果与交叉连接相同 select * from student join class on student.cid = class.cid; select * from student inner join class on student.cid = class.cid;
select * from student left join class on student.cid = class.cid;
select * from student right join class on student.cid = class.cid;
select * from student natural join class;
select * from student natural left join class;
select * from student cross join class; select * from student,class;
select * from student inner join class on student.cid = class.cid;-- 原本结果 select id,name,gender,c.cid,cname from student as s inner join class as c on s.cid = c.cid;-- 使用表别名
select name,gender from student union select * from class; -- 因为class就两个字段,所以第一个只选出两个字段
-- 这是一个无意义的例子。仅为举例使用 select cid,cname from (select * from class where cname="python") as c;
select * from student where cid=(select cid from class where cname="python");
-- 这是一个如果学生没有选择cid=1的课,那么不输出对应课程信息的例子 select * from class where exists(select * from student where cid=1) and cid=1;
看完上述内容,是不是对如何使用mysql连接查询、联合查询、子查询有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。