SQL实例整理
本文适合将w3school的SQL教程(http://www.w3school.com.cn/sql/sql_create_table.asp)都基本看过一遍的猿友阅读。
说说博主的情况吧。毕业找工作之前确实有大概看过w3school的SQL教程,然后参加校园招聘,每次遇到一些SQL笔试题,立马懵逼了(大写的)。其实我那时候大概知道怎么写的,只是总是写不正确,或者是对一些特定的而且没有见过的场景的SQL语句,根本写不出来。相信不少猿友工作之后,其实挺多都用得不熟吧(如果白板编写的话)。
因为大部分Java猿友工作做的事情,其实比较少情况自己去动手写特定场景的SQL(可能有也是百度,接触过一个会一个),简单SQL也是直接由框架(hibernate和Mybatis)提供接口。当然,那种专门做后台,经常跟数据打交道的Java猿友除外,因此只能说大部分。
如果还是继续保持这样的状态的话,下次自己找工作遇到SQL笔试题,估计也会继续懵逼(大写的)。
下面小宝鸽整理了一些实例(实例主要来自网上),以提升自己写SQL的某些关键字的理解。
1、用一条SQL 语句 查询出每门课都大于80 分的学生姓名。(表结构如下图)
答案可以有如下两种:
select distinct student_name from table_test_one where student_name not in
(select distinct student_name from table_test_one where score<=80);
或者
select student_name from table_test_one group by student_name having min(score)>80;
第二种方法是group by 、min函数 结合 having的使用,w3school教程里面也提到过(在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用)
似乎看懂了,但是还是没有自己运行一遍深刻!!!自己能动手敲一遍就更好了!
下面我们自己造数据,后面的例子也会用到。
建表然后倒入初始数据:
DROP TABLE IF EXISTS `table_test_one`;
CREATE TABLE `table_test_one` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`student_no` varchar(10) NOT NULL,
`student_name` varchar(10) NOT NULL,
`subject_no` varchar(10) NOT NULL,
`subject_name` varchar(10) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
INSERT INTO `table_test_one` VALUES ('1', '201601', '张三', '0001', '数学', '98');
INSERT INTO `table_test_one` VALUES ('2', '201601', '张三', '0002', '语文', '66');
INSERT INTO `table_test_one` VALUES ('3', '201602', '李四', '0001', '数学', '60');
INSERT INTO `table_test_one` VALUES ('4', '201602', '李四', '0003', '英语', '78');
INSERT INTO `table_test_one` VALUES ('5', '201603', '王五', '0001', '数学', '99');
INSERT INTO `table_test_one` VALUES ('6', '201603', '王五', '0002', '语文', '99');
INSERT INTO `table_test_one` VALUES ('7', '201603', '王五', '0003', '英语', '98');
可以运行一下上面两个语句试试结果是不是你想要的。
2、删除除了id不同, 其他都相同的学生冗余信息,表如下:
答案:
delete table_test_one where id not in
(select min(id) from table_test_one group by
student_no, student_name, subject_no, subject_name, score);
是否有看懂?如果没能看懂的话,继续往下看:
先来造数据,题1中的数据只需要执行如下SQL就变成题2中的数据了:
update table_test_one set subject_no = '0001', subject_name = '数学' where id = 6;
然后我们先执行这个看看:
select min(id) from table_test_one group by
student_no, student_name, subject_no, subject_name, score
这个的执行结果如下:
如果还不懂就再看看几次吧。
PS:GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组。刚刚就是GROUP BY 对多列的使用场景。
3、行转列:
表数据如下:
希望查询到结果如下:
答案:
select year,
(select amount from table_test_two t where t.month = 1 and t.year = table_test_two.year) as month1,
(select amount from table_test_two t where t.month = 2 and t.year = table_test_two.year) as month2,
(select amount from table_test_two t where t.month = 3 and t.year = table_test_two.year) as month3
from table_test_two group by year;
利用group by 实现行转列,这种场景在数据统计的时候经常用到。
猿友可以造数据自己运行试试:
-- ----------------------------
-- Table structure for `table_test_two`
-- ----------------------------
DROP TABLE IF EXISTS `table_test_two`;
CREATE TABLE `table_test_two` (
`year` int(11) NOT NULL,
`month` int(11) NOT NULL,
`amount` decimal(10,1) NOT NULL,
PRIMARY KEY (`year`,`month`,`amount`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of table_test_two
-- ----------------------------
INSERT INTO `table_test_two` VALUES ('1991', '1', '1.1');
INSERT INTO `table_test_two` VALUES ('1991', '2', '1.2');
INSERT INTO `table_test_two` VALUES ('1991', '3', '1.3');
INSERT INTO `table_test_two` VALUES ('1992', '1', '2.1');
INSERT INTO `table_test_two` VALUES ('1992', '2', '2.2');
INSERT INTO `table_test_two` VALUES ('1992', '3', '2.3');
4、复制表( 只复制结构, 源表名:table_test_two 新表名:table_test_three)
答案:
create table table_test_three as
select * from table_test_two where 1=2;
PS:如果需要将数据也复制过去,则上面改成where 1=1
5、复制表数据(将表 table_test_two 的数据复制到表table_test_three 里面)
答案:
insert into table_test_three (year,month,amount)
select year,month,amount from table_test_two;
6、两张关联表,删除主表中已经在副表中没有的信息
答案:
delete from table_test_student where not exists
(select * from table_test_class where table_test_student.class_id = table_test_class.calss_id);
我们先造点数据吧:
-- ----------------------------
-- Table structure for `table_test_class`
-- ----------------------------
DROP TABLE IF EXISTS `table_test_class`;
CREATE TABLE `table_test_class` (
`calss_id` int(11) NOT NULL AUTO_INCREMENT,
`calss_name` varchar(10) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`calss_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of table_test_class
-- ----------------------------
INSERT INTO `table_test_class` VALUES ('1', '一班');
-- ----------------------------
-- Table structure for `table_test_student`
-- ----------------------------
DROP TABLE IF EXISTS `table_test_student`;
CREATE TABLE `table_test_student` (
`student_id` int(11) NOT NULL AUTO_INCREMENT,
`student_name` varchar(10) CHARACTER SET utf8 NOT NULL,
`class_id` int(11) NOT NULL,
PRIMARY KEY (`student_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
-- ----------------------------
-- Records of table_test_student
-- ----------------------------
INSERT INTO `table_test_student` VALUES ('1', '罗国辉', '1');
INSERT INTO `table_test_student` VALUES ('2', '小宝鸽', '2');
执行后数据如下:
显然副表student中小宝鸽这条数据的calss_id,主表没有对应的class_id.
执行对应SQL语句就会把小宝鸽这条数据删除掉了。
未完待续……….(TODO),边学习边写博客真的很花时间,累并快乐着~~~
SQL实例整理的更多相关文章
- 常用sql语句整理:mysql
## 常用sql语句整理:mysql1. 增- 增加一张表```CREATE TABLE `table_name`( ... )ENGINE=InnoDB DEFAULT CHARSET=utf8 ...
- Oracle数据库常用的Sql语句整理
Oracle数据库常用的Sql语句整理 查看当前用户的缺省表空间 : select username,default_tablespace from user_users; 2.查看用户下所有的表 : ...
- 搜索本地网络内所有可用的SQl实例
'搜索本地网络内所有可用的SQl实例 Dim instance As SqlDataSourceEnumerator = SqlDataSourceEnumerator.Instance Dim dt ...
- 一个项目涉及到的50个Sql语句(整理版)
/* 标题:一个项目涉及到的50个Sql语句(整理版) 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句. */ --1.学生表Student(S,Sname,Sage,Sse ...
- JAVA8 in Action:行为参数化,匿名类及lambda表达式的初步认知实例整理
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.functio ...
- [转]SQL SERVER整理索引碎片测试
SQL SERVER整理索引碎片测试 SQL SERVER整理索引的方法也就这么几种,而且老是自作聪明的加入智能判断很不爽,还是比DBMS_ADVISOR差远了: 1SQL SERVER 2000/2 ...
- SQL代码整理
--SQL代码整理: create database mingzi--创建数据库go--连接符(可省略)create table biao--创建表( lieming1 int not null,-- ...
- 教你管理SQL实例系列(1-15)
全系列转自:51CTO ->jimshu http://jimshu.blog.51cto.com 目录及原本连接如下: 教你管理SQL实例(1)数据库实例 教你管理SQL实例(2)服务启动帐户 ...
- 学习《Oracle PL/SQL 实例讲解 原书第5版》----创建账户
通过readme.pdf创建student账户. 以下用sys账户登录时都是sysdba. 一.PL/SQL 登录oracle. SYS/123 AS SYSDBA 账户名:sys:密码:123:作 ...
随机推荐
- Genymotion下载慢或者下载失败的解决办法
转.原文地址:http://blog.csdn.net/sean_css/article/details/52674091 办法如下: 1.首先点击界面上的 + 号(Add)按钮,选择你要下载的模拟器 ...
- [LeetCode] Design TinyURL 设计精简URL地址
Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...
- [LeetCode] Next Greater Element III 下一个较大的元素之三
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- js高阶函数应用—函数柯里化和反柯里化
在Lambda演算(一套数理逻辑的形式系统,具体我也没深入研究过)中有个小技巧:假如一个函数只能收一个参数,那么这个函数怎么实现加法呢,因为高阶函数是可以当参数传递和返回值的,所以问题就简化为:写一个 ...
- PHP观察者模式与Yii2.0事件
1.先看PHP观察者模式的实现: 想要使用事件.必须实现事件的基类.统一的addObserver和trigger方法 定义统一接口.所有的观察者都要实现此接口 //事件的基类 abstract cla ...
- BAT 前端开发面试 —— 吐血总结
更好阅读,请移步这里 聊之前 最近暑期实习招聘已经开始,个人目前参加了腾讯和阿里的内推及百度的实习生招聘,在此总结一下 一是备忘.总结提升,二是希望给大家一些参考 其他面试及基础相关可以参考其他博文: ...
- Centos常用命令之:VI
在Linux中,对文件内容的编辑莫过去vi命令了,它是每个发布版本中的标配.并且功能强大. 在vi中一共有三种模式,一般模式(命令参照),编辑模式(命令参照)与命令模式(命令参照). ◇一般模式:当我 ...
- [COGS 2401]Time is Money
Description 题库链接 给你 \(n\) 个节点 \(m\) 条边的无向连通图.每条边有两个权值 \(c,t\) ,要你生成一棵边集为 \(\mathbb{E}\) 的生成树使得 \[\su ...
- NOIP 2015
Prob.1 2015 神奇的幻方 模拟就好了.(这不是noip2017的初赛题么.)代码: #include<cstdio> #include<cstring> #inclu ...
- hdu 5113(2014北京—搜索+剪枝)
题意:有N*M的棋盘,用K种颜色去染,要求相邻块不能同色.已知每种颜色要染的块数,问能不能染,如果能,输出任一种染法. 最开始dfs失败了- -,优先搜索一行,搜完后进入下一列,超时.本来以为搜索不行 ...