您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了如何使用mysql连接查询、联合查询、子查询,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
连接查询:
-- 实验表结构 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连接查询、联合查询、子查询有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。