
SQL 查询强化 - 数据准备
发布日期:2021-05-09 01:15:08
浏览次数:17
分类:博客文章
本文共 4027 字,大约阅读时间需要 13 分钟。
最近要搞新的项目了, 我的 BI 报表这块, 我感觉, 可能又要写sql, 对于一些简单的 查询, 表连接我还应付得来, 如果涉及多个表的, 什么子查询嵌套, 自定义函数, 加上控制流...就感觉就不行了, 下的我赶紧从网上找几篇教程来跟着写一写...
SQL 真的是, 特别, 特别重要, 面试,笔试必考... 而我是专做数据这块, 必然是重中之重. 而然,我真实中却, 真的写的很少, 主要是我太依赖 Pandas 了, 直接连接上多个表, 然后就可以为所欲为, 其实跟sql 是一样的, 什么段筛选, 表横向连接 (join) , 纵向连接 (union) , 分组聚合 (group by, aggregation) + 控制流(if else, loop) 80% 的数据处理内容, 就是这些呀.
我在处理这些内容时, 用编程语言如 Python 来搞, 轻轻松松, 再复杂的逻辑, 我无所畏惧. 但一旦要用 sql 写出逻辑来, 我真的是很头疼, 而且吧, 不同的 sql 写法又不一样(有差异)标准sql, mysql, oracle, sqlserver, 以及目前公司用的 sybase sql... 我好难...
但必须迈过这道坎. (就以mysql 为栗子来整吧, 反正大致也差不多的).
表关系
还是咱最为熟悉的, 学生表, 成绩表, 课程表, 教师表... 业务也是差不多的, 只是熟悉度而已, 我感觉.
数据准备
-- 创建库-- 终端: pysql -u root -p (回车)-- 密码: xxxxshow databases;drop database if exists cj;create database cj charset=utf8;use cj;-- 1. 学生表 student (s_id, s_name, birth_date, gender)drop table if exist student;create table student( s_id char(5), s_name varchar(20), birth_date date, gender char(3)); -- 插入学生数据insert into student(s_id,s_name,birth_date,gender)values('0001' , '王二' , '1989-01-01' , '男');insert into student(s_id,s_name,birth_date,gender)values('0002' , '星落' , '1990-12-21' , '女');insert into student(s_id,s_name,birth_date,gender)values('0003' , '胡小适' , '1991-12-21' , '男');insert into student(s_id,s_name,birth_date,gender)values('0004' , '油哥' , '1996-10-01' , '男');-- 2. 成绩表 score (s_id, c_id, score)drop table if exists score;create table score( s_id char(5), c_id char(5), score int);-- 插入学生成绩insert into score(s_id,c_id,score)values('0001' , '0001' , 80);insert into score(s_id,c_id,score)values('0001' , '0002' , 90);insert into score(s_id,c_id,score)values('0001' , '0003' , 99);insert into score(s_id,c_id,score)values('0002' , '0002' , 60);insert into score(s_id,c_id,score)values('0002' , '0003' , 80);insert into score(s_id,c_id,score)values('0003' , '0001' , 80);insert into score(s_id,c_id,score)values('0003' , '0002' , 80);insert into score(s_id,c_id,score)values('0003' , '0003' , 80);-- 3. 课程表 course (c_id, c_name, t_id)drop table if exists course;create table course( c_id char(5), c_name varchar(20), t_id char(5));-- 插入课程信息insert into course(c_id,c_name,t_id)values('0001' , '语文' , '0002');insert into course(c_id,c_name,t_id)values('0002' , '数学' , '0001');insert into course(c_id,c_name,t_id)values('0003' , '英语' , '0003');-- 4.教师表 teacher (t_id, t_name)drop table if exists teacher;create table teacher( t_id char(5), t_name varchar(20));-- 插入教师信息insert into teacher(t_id,t_name)values('0001' , '欧拉');insert into teacher(t_id,t_name)values('0002' , '仲尼');-- 这里的t_name是空值(null)insert into teacher(t_id,t_name)values('0003' , null);-- 这里的t_name是空字符串('')insert into teacher(t_id,t_name)values('0004' , '');
数据预览
mysql> show tables;+--------------+| Tables_in_cj |+--------------+| course || score || student || teacher |+--------------+4 rows in set (0.00 sec)
学生表 student
mysql> select * from student;+------+-----------+------------+--------+| s_id | s_name | birth_date | gender |+------+-----------+------------+--------+| 0001 | 王二 | 1989-01-01 | 男 || 0002 | 星落 | 1990-12-21 | 女 || 0003 | 胡小适 | 1991-12-21 | 男 || 0004 | 油哥 | 1996-10-01 | 男 |+------+-----------+------------+--------+4 rows in set (0.00 sec)
成绩表 score
mysql> select * from score;+------+------+-------+| s_id | c_id | score |+------+------+-------+| 0001 | 0001 | 80 || 0001 | 0002 | 90 || 0001 | 0003 | 99 || 0002 | 0002 | 60 || 0002 | 0003 | 80 || 0003 | 0001 | 80 || 0003 | 0002 | 80 || 0003 | 0003 | 80 |+------+------+-------+8 rows in set (0.00 sec)
课程表 course
+------+--------+------+| c_id | c_name | t_id |+------+--------+------+| 0001 | 语文 | 0002 || 0002 | 数学 | 0001 || 0003 | 英语 | 0003 |+------+--------+------+3 rows in set (0.00 sec)
教师表 teacher
mysql> select * from teacher;+------+--------+| t_id | t_name |+------+--------+| 0001 | 欧拉 || 0002 | 仲尼 || 0003 | NULL || 0004 | |+------+--------+4 rows in set (0.00 sec)
后面的查询, 都基于这个数据, 字段啥的. 当然这也是咱最为熟悉的表. 哎, 真的是孰能生巧吧, 这种东西.
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月24日 01时03分33秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
中序线索二叉树的遍历
2019-03-15
文字策略游戏 android studio(学习intent,textview,等等)
2019-03-15
laravel server error 服务器内部错误
2019-03-15
17_注册Github账号
2019-03-15
Linux驱动实现GPIO模拟I2C读写操作
2019-03-15
iJ配置Maven环境详解
2019-03-15
仿QQ登陆界面
2019-03-15
HttpServletResponse-完成文件下载
2019-03-15
什么题目的暂时还没想好
2019-03-15
Python中pip安装模块太慢
2019-03-15
docker安装
2019-03-15
N皇后问题解法(递归+回朔)
2019-03-15
面试题 08.01. 三步问题
2019-03-15
剑指 Offer 11. 旋转数组的最小数字
2019-03-15
word文档注入(追踪word文档)未完
2019-03-15
作为我的第一篇csdn博客吧
2019-03-15
java中简单实现栈
2019-03-15
ajax异步提交失败
2019-03-15
一道简单的访问越界、栈溢出pwn解题记录
2019-03-15