找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手

多表连接笔记与总结(第7天)

[复制链接]
本帖最后由 严上 于 2019-8-27 20:12 编辑

多表连接     重点值MAX
1.“navicat”数据库客户端连接工具
MYSQL的注释://这是在使用“navicat”数据库客户端连接工具的“查询编辑器”窗口使用的
行注释:#
连续多行注释:/*``````*/
段注释:/*


2:多表连接:2个或2个以上的表进行连接之后进行的查询
create table aa(id int(1) PRIMARY key,name char(20));
create table cc(s_id int(1) PRIMARY key,score char(20));


insert into aa(id,name)values(1001,'zhangsan'),(1002,'lisi');
insert into cc(s_id,score)values(1001,'99');

select * from aa;
select * from cc;

select * from aa,cc where aa.id=cc.s_id;   //基本连接查询
select * from aa inner join cc on aa.id=cc.s_id; //内连接
select * from aa left join cc on aa.id=cc.s_id;  //左连接查询

select * from aa left join cc on aa.id=cc.s_id; //左连接   
+------+----------+------+-------+
| id   | name     | s_id | score |
+------+----------+------+-------+
| 1001 | zhangsan | 1001 |    99 |
| 1002 | lisi     | NULL | NULL  |
+------+----------+------+-------+
select * from aa right JOIN cc on aa.id=cc.s_id;//右连接
+------+----------+------+-------+
| id   | name     | s_id | score |
+------+----------+------+-------+
| 1001 | zhangsan | 1001 |    99 |
+------+----------+------+-------+

select * from aa union select * from cc;   //union连接(前提条件,多个关系表的表字段数目必须相同)
+------+----------+
| id   | name     |
+------+----------+
| 1001 | zhangsan |
| 1002 | lisi     |
| 1001 | 99       |
+------+----------+
select name,score from (select * from aa,cc where aa.id=cc.s_id)as yy  //求出张三的成绩 ---临时表
+------+----------+
| name  | score     |
+------+----------+
| zhangsan | 99 |

select score from aa,cc where aa.id=cc.s_id and name='zhangsan';//求出张三的成绩
select score from cc where s_id=(select id from aa where name='zhangsan');//求出张三成绩 --- 嵌套方法
select score from cc where s_id in(select id from aa where name='zhangsan');//求出张三成绩 --- 嵌套方法

17.聚合函数的使用
select class,avg(math) as avg_math from student group by class having avg_math>80; //哪一些班级的数学平均成绩大于80

select class,max(math) from student group by class;   //按班级查出数学最高分;
select class,min(math) from student group by class;  //查询出每班数学最低分
select class,sum(math) from student group by class; //查询出每班数学总分
select class,avg(math) from student group by class; //查询出每班数学平均分
select class,count(*) from student group by class; //查询出每班学生总数
注:group by后面接的字符段一定要是select后面有的(class)

数据库的“-,+、*、/”
select sum(math)+5 from student; //在数学总分上加‘5’分
select sum(math)-5 from student;
select sum(math)*5 from student;
select sum(math)/5 from student;

模糊匹配:
select * from student where name like 'wang%' //查询姓名“wang”开头的用户
select * from student where name like '%qi'   //查询姓名“qi”结尾的用户
select * from student where name like '%ao%'  //查询姓名中间包含“ao”用户


1.where 不能放在group by 后面
where 子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数比如SUM(),AVG()等,使用where条件显示特定的行。

2.having 是跟在group by  后面一起用的,放在group by  后面,此时的作用相当于where
     having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having 条件显示特定的组,也可以使用多个分组标准进行分组。



总结:
嵌套表、临时表、基本连接表的区别
嵌套表:select score from cc where s_id=(select id from aa where name='zhangsan')      
                       由某个字段=或者in()的
临时表:select name,score from (select * from aa,cc where aa.id=cc.s_id)as yy
                      使用了as进行了另命名的
基本连接表:select * from aa,cc where aa.id=cc.s_id;
                      没啥好讲的,基本连接表还是很明显的

多表连接中的语句顺序:
select--from--where--group by--having--order by— limit
意思就是类似于语言类中的语法顺序:xxx只能跟在xxx后面

多表连接方法(颜色是相对应的)
最熟悉的方法:
select+所求的问题+from+临时表+where+条件
eg:查出财务部门工资少于3800元员工姓名
    select name from (select * from dept,emp where dept.dept1=emp.dept2 )t where incoming <3800 and dept_name='caiwu'

注:(1)临时表解释:dept和emp为需要拼接的表,其条件为两个表必须有一个相同数据的字符(这
              两个表相同数据的字符段为:dept表中的dept1字符段(dept.detp1),emp表中的dept2字符        
              段(emp.dept2)),同时要给临时表赋值(在该例子中为t)

        (2)在临时表后面的条件限制中,不仅限于使用where,需要注意的是要先弄清楚条件限制的先后
              顺序,比如:先用where限制条件再使用group by分组 或者 先使用group by进行分组再使用   
              having限制条件

       (3)在where或者having后面是可以接嵌套表
            eg:******* where incoming=(select max(incoming) from emp ) and dept_name='caiwu'
            此时incoming的输出等于 emp表中max(incoming)的值


特殊方法:
基本连接表  {     (临时表1) , (临时表2)      }+where+条件

eg:列出每一个部门中年纪最大的员工姓名,部门名称
select name,dept_name from (select * from dept,emp where dept.dept1=emp.dept2)a , (select dept2,max(age) as b from emp group by dept2)c where a.dept1=c.dept2 and a.age=c.b

注:(1)临时表1解释:将两个表拼接起来,在这个方法中是必须要有的!!(具体解释见熟悉方法)

         (2)临时表2解释:实质上是一个条件。在该临时表中所求的条件为detp2和max(age),max(age)不难理解,那为什么求dept2呢?其实求dept2有两个原因:1、后面需要group by进行分组 2、因为后面要进行临时表的连接(where a.dept1=c.dept2),临时表1和临时表2的唯一相同数据的字符段就是dept1和dept2(临时表连接的条件要有两个表数据相同的字符段),所以临时表1和临时表2的输出必须都要有相同数据的字符段(dept1和dept2)

        (3)基本连接解释:这个基本连接表囊括了临时表1和临时表2的输出,然后把临时表1和临时表2的输出再进行拼接,组成我们所求的表格(因为数据库中的select操作,无论输入和输出都是表格)

       (4)where+条件:这只是一种表达条件的方式,并不是只能where+条件,视具体情况而定


问题:一般情况下都是使用的熟悉方法,但是有些时候需要用到特殊方法,问题在于我有点分不清哪些情况使用特殊方法去解?有大哥可以说一下吗?在用熟悉方法做不出来的时候,我往往会纠结很久是不是语法有问题或者语句顺序问题等等,从而耗费了很多时间,但是又分不清哪些情况用熟悉方法是做不出来的。。。。郁闷

这次是真材实料了啊,不水贴了


分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册