Python学习笔记十九(MySQL、SQL、查找、单表查询)

查找

查找分为单表查询与多表查询

单表查询

查看现有数据表

01现有数据库.png

查看所有数据

-- select * from 表名; 查找指定表的所有数据
select * from article;
02查看所有数据.png

查看某些字段

比如我只关心title 字段 或者 我只关心title 字段 和content_num 字段

-- select 字段名【,字段名,字段名...】 from 表名 ;
select title from article; -- 只关心title
select title,content_num from article; -- 只关心title 和 content_num
03查看指定字段.png

给字段起别名

如果将查出来的结果导出到文件,别人看到title 或者 content_num 可能不知道是什么意思,那怎么解决这个问题?答案是别名,什么是别名?可以理解为昵称,给字段起一个昵称,需要使用as 关键字。

select title as "标题" from article;  
select title as "标题",content_num as "字数" from article;  
04字段别名.png

注:别名只对当前语句有效

05别名的作用范围.png

以上为SQL 的特殊语法,可以忽略,重点与我们的预期不符,标题是title 的别名,但是没得到相关的数据。所以别名只对当前语句(起别名的语句)有效。

给表起别名

能给字段起别名,能不能给表起别名?答案是可以,验证一下。

select title as "标题" from article as arti;  
select title as "标题",content_num as "字数" from article as arti;  
06表别名.png

SQL条件语句

条件语句需要where 关键字,有哪些条件呢?
先处理下一下数据备用。

alter table article modify content_file_path  varchar(100) default "/article/details/80244167";

insert into article(title,content_num ) values("Python",1000),("python1",500),("python2",1000),("python3",1500),("python4",2000),("python5",2500),("python6",3000),
("python7",3500),("python8",4000),("python9",4500),("python10",5000),("python11",5500),
("python12",6000),("python13",6500),("python14",7000),("python15",7500),("python16",8000),
("python17",8500),("python18",9000),("python19",9500);
07处理数据备用.png
比较运算[1]
  • 等于: =
-- 查看数据content_num 为4000的数据  
-- select 字段名【,字段名,字段名...】 from 表名 【where 条件】;
select * from article where content_num =4000;
-- 查看数据content_num 为1000的整数倍的数据
-- 数值类型支持+、-、*、/、%
select * from article where content_num%1000 =0; 
08等于.png
  • 大于: >
-- content_num 大于8000
select * from article where content_num>8000; 
09大于.png
  • 大于等于: >=
-- content_num 大于等于8000
select * from article where content_num>=8000; 
10大于等于.png
  • 小于: <
-- content_num 小于8000
select * from article where content_num<8000; 
11小于.png
  • 小于等于: <=
-- content_num 小于等于8000
select * from article where content_num<=8000; 
12小于等于.png
  • 不等于: != 或 <>
-- content_num 不等于5201314, <> 和 != 一样所以只验证一个
select * from article where content_num!=5201314; 
13不等于.png
  • 逻辑运算符
  • and
-- and 数据的交集,只有and 左右两边的条件都满足的数据才会被显示出来
-- 最终结果取每个条件单独成立时,数据记录结果集的公共部分组成
-- title=YanglingWang content_num=5201314
select * from article where  title="YanlingWang" and content_num="5201314";
14AND.png
  • or
-- or 数据的并集,只要or 两边的条件有一边成立就会有数据显示
-- 最终结果由每个条件单独成立时,数据结果集的去重集合组成
-- title=Python5 content_num=8000  **数值型的值也可以使用"引号"包裹
select * from article where  title="Python5" or content_num="8000";
15OR.png
  • not
-- not 数据的差集,或者取反,只有not 右边的条件不满足是才会有数据显示
-- 最终结果由表中的数据记录去除条件成立的结果集组成,既取反,对于表取条件成立的反结果集
-- 取所有content_num 不能被10 整除的数据 
select * from article where not (content_num%10=0);
16NOT.png
  • 模糊查询

  • 关键字 like

  • 通配符%:表示任意多个任意字符

  • 通配符_: 表示一个任意字符

-- 查询所有title中含有W 字母的数据,W前后可以有任意字符
select * from article where  title like "%W%";
17like与%.png
-- 查询所有content_num 中所有含幸运数字5 的数据,并且5在百位 
select * from article where  CAST(title as varchar(20)) like "%5__";
18like与_.png


模糊不能对int 进行直接查找,所以使用cast[2] 对字段进行一个临时转化
怎样查找含有% 或者_ 的字符串?使用 escape 转义[3] , ( like '/_fang' escape '/' ) 匹配 _fang

  • 范围查询
    • in表示在一个非连续的范围内
    • between ... and ...表示在一个连续的范围内
-- in 多个独立条件成立的并集
select * from article where title in("Python1","Python3","Python5");
-- between ... and ... 连续范围,等价于 >= and <=
select * from article where content_num between 8000 and 10000;
19范围查找.png
  • 空判断
    • null 是一个特殊的值,不能使用= 判断
    • 判断为空is null
    • 判断不为空
-- 查找title 不为null 并且content_num 小于2000 的数据
select * from article where (title is not null) and content_num < 2000;
-- 查找file_path 为null 的数据
select * from article where file_path is null;
select * from article where file_path = null; 
select * from article where file_path = "null";
20判断是否为Null.png

排序

  • 关键字 order by
  • 升序 asc :从小到大排序,默认asc排序
  • 降序 desc : 从大到小排序
-- 查找content_num 大于 8000的数据,按照content_num 从大到小排序
-- order by 字段 desc(asc) 
select * from article where content_num >8000 order by content_num desc;
21排序.png

聚合函数[4]

  • count(*) 统计有多少记录,指定字段如果有null 值,不计数
select count(*) from article; 
22count.png
  • max(字段) 指定字段的最大值
  • min(字段) 指定字段的最小值
-- 查看content_num 的最大值
select max(content_num ) from article;
-- 查看content_num 的最小值
select min(content_num ) from article;
23max_min.png
  • sum(字段) 计算总和,null 不计算
  • avg(字段) 计算平均值,null 算入分母
-- 查看content_num 的总和
select max(content_num ) from article;
-- 查看content_num 的最小值
select min(content_num ) from article;
24sum_avg.png
  • round() 四舍五入
-- 将平均值保留两位小数
-- round(字段,小数位数)
select round(avg(content_num),2) from article;
25round.png

分组

  • group by 将查询结果按照1个或多个字段进行分组,字段值相同的为一组
  • group_concat() 显示分组内数据
-- 按照content_num 分组,使用group_concat 显示分组内的title,对结果集排序
select content_num,group_concat(title) from article group by content_num desc;
26group by 排序.png
  • group by + 集合函数
  • group by + having
-- 按照content_num 分组,使用count 显示每组的个数
select content_num,count(*) from article group by content_num ;
-- 按照content_num 分组,使用having 只显示count >=2的数据
select content_num,count(*) from article group by content_num having count(*)>=2;
27group by 聚合函数.png

分页

  • 关键字 limit
  • limit start,count
-- 显示部分行limit 开始位置,每页显示的内容数
-- 推算开始位置,当每页显示内容固定时,每一页的开始位置=(页数-1)*内容数
select * from article limit 0,5;  -- 第一页
select * from article limit 5,5;  -- 第二页 
select * from article limit 10,5;  -- 第三页 
27limit.png

到此结 DragonFangQy 2018.5.13


  1. MySQL运算符

  2. CAST与CONVERT

  3. escape 转义

  4. 聚合函数

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容