当前位置:   article > 正文

sql学习_sql insert select union all语句

sql insert select union all语句

参考文章:https://www.cnblogs.com/qixuejia/p/3637735.html

创建表

  1. CREATE TABLE student
  2. (
  3. s# INT,
  4. sname nvarchar(32),
  5. sage INT,
  6. ssex nvarchar(8)
  7. )
  8. CREATE TABLE course
  9. (
  10. c# INT,
  11. cname nvarchar(32),
  12. t# INT
  13. )
  14. CREATE TABLE sc
  15. (
  16. s# INT,
  17. c# INT,
  18. score INT
  19. )
  20. CREATE TABLE teacher
  21. (
  22. t# INT,
  23. tname nvarchar(16)
  24. )

插入数据:

  1. insert into Student select 1,N'刘一',18,N'男' union all
  2. select 2,N'钱二',19,N'女' union all
  3. select 3,N'张三',17,N'男' union all
  4. select 4,N'李四',18,N'女' union all
  5. select 5,N'王五',17,N'男' union all
  6. select 6,N'赵六',19,N'女' ;
  7. insert into Teacher select 1,N'叶平' union all
  8. select 2,N'贺高' union all
  9. select 3,N'杨艳' union all
  10. select 4,N'周磊';
  11. insert into Course select 1,N'语文',1 union all
  12. select 2,N'数学',2 union all
  13. select 3,N'英语',3 union all
  14. select 4,N'物理',4;
  15. insert into SC
  16. select 1,1,56 union all
  17. select 1,2,78 union all
  18. select 1,3,67 union all
  19. select 1,4,58 union all
  20. select 2,1,79 union all
  21. select 2,2,81 union all
  22. select 2,3,92 union all
  23. select 2,4,68 union all
  24. select 3,1,91 union all
  25. select 3,2,47 union all
  26. select 3,3,88 union all
  27. select 3,4,56 union all
  28. select 4,2,88 union all
  29. select 4,3,90 union all
  30. select 4,4,93 union all
  31. select 5,1,46 union all
  32. select 5,3,78 union all
  33. select 5,4,53 union all
  34. select 6,1,35 union all
  35. select 6,2,68 union all
  36. select 6,4,71;

第一次见到insert into table  select    *,*,*,*,* union all *,*,*,*,* 的sql语句

insert select 这样的语句支持多条数据插入

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。

请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。

union all支持重复的数据插入,想要不重复就用union代替union all,union会自动过滤掉重复的数据

  1. -- 查询“1”课程比“2”课程成绩高的所有学生的学号;
  2. select a.s_id from (select s_id,score from sc where c_id='1' ) a,(select s_id,score from sc where c_id='2') b where a.score > b.score and a.s_id = b.s_id;
  3. 解析:分别查出课程为12的所有的数据,当课程1的sid=课程2的id并且课程1分数大于课程2分数的时候就展示s_id
  1. -- 2、查询平均成绩大于60分的同学的学号和平均成绩;
  2. select s_id ,avg(score) FROM sc GROUP BY s_id HAVING avg(score)>60;
  3. 解析:先用group by(s_id)将s_id和平均成绩查询出来,再用having avg(score) 筛选60分以上的成绩
  1. -- 3.查询所有同学的学号、姓名、选课数、总成绩;
  2. select a.s_id 学号,b.sname 姓名,a.number 选课数,a.sunScore 总成绩 from (SELECT s_id,count(c_id) number,SUM(score) sunScore FROM sc GROUP BY s_id) a ,student b where a.s_id = b.s_id;
  3. 解析:先用group by s_id 查出SELECT s_id,count(c_id) number,SUM(score) sunScore FROM sc GROUP BY s_id 学生的id,选课数,总成绩. 然后关联两个表查询名字相同的学生
  4. select student.s_id,student.sname,count(sc.c_id),sum(score) from student LEFT JOIN sc on student.s_id = sc.s_id GROUP BY student.s_id;
  5. 解析:此时用到了left join 此时我们来分析一下left join ,right joininner join
  6. 现在有两个表
  7. a表 b表
  8. id name id age
  9. 1 张三 1 22
  10. 2 李四 3 3
  11. select * from a left join b on a.id=b.id;
  12. 展示结果如下
  13. 1 张三 1 22
  14. 2 李四 null null
  15. select * from a right join b on a.id=b.id;
  16. 展示结果如下
  17. 1 张三 1 22
  18. null null 3 30
  19. 但是如果是inner join
  20. select * from a inner join b on a.id=b.id; 相当于 select * from a,b where a.id=b.id;
  21. 结果只展示相同的数据,如下:
  22. 1 张三 1 22

 

  1. -- 4、查询姓“李”,名字不相同的老师的个数;
  2. select count(distinct(tname)) from teacher where tname like '李%';
  3. 解析;第一次看到count中写distinct去重,可以查询姓李的且名字不相同的人数

 

面试总结:

AOP是面向切面编程,例如日志打印,封装好之后供其他调用,降低代码重复。oop是AOP的前身:面向对象编程,但是只能一级一级的使用,不能横向使用。

利用AOP技术实现权限拦截和系统监控等


CMPP2.0和3.0  实现直连移动运营商,心跳检测5秒一次保持长连接,三次握手,connect  submit deliver阶段,定长报文

线程  extends Thread   run启动运行   implements runnable  run方法 

sleep(毫秒) 不释放对象锁 当时间结束没有其它线程运行则就会自动运行
wait() 释放对象锁,必须要用notify方法才能唤醒该线程。


IO流
字节流:

inputStream  
FileInputStream
BufferedInputStream

OutputStream
FileOutputStream
BufferedOutputStream

字符流:

Reader
InputStreamReader
FileReader
BufferedReader

Writer
OutputStreamWriter
FileWriter
BufferedWriter


印象最深的就是写出时调用flush(),除了close()方法,还有flush方法,就是把在内存中的为满足写出内存的立马写出。char[] buf = new char[1024];

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/707719
推荐阅读
相关标签
  

闽ICP备14008679号