mysql连接查询、联合查询、子查询原理与用法实例详解
数据库  /  管理员 发布于 6年前   491
本文实例讲述了mysql连接查询、联合查询、子查询原理与用法。分享给大家供大家参考,具体如下:
首发日期:2018-04-11
-- 实验表结构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中没有的4insert into class values(1,"linux"),(2,"python"),(3,"java"),(5,"html5");--这里特意创建了一个student中没有的5select * 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 studentunion 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相关内容感兴趣的读者可查看本站专题:《MySQL查询技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》、《MySQL数据库锁相关技巧汇总》及《MySQL常用函数大汇总》
希望本文所述对大家MySQL数据库计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号