# 增(一共有三种方式)

    # 插入单条记录
insert into t1(name,...) values('lzp',..);
注意一点:t1(name,...)必须包含所有非空列(除去自增列) # 插入多条记录
insert into t1(name,age) values('lzp',),('alex',); # 拷贝复制(拷贝t2表的数据到t1)
insert into t1(name,age) values select name,age from t2; # 改
update t1 set name='b',age= where name='lzp';
update t1 set name='c' where name='alex'; # 查(重点) select [...] from tb [...] 从前面[]扩展:
select name as cname, age from tb1; # 起别名
select name, age, from tb1; # 多显示一列,该列的值为常量
select name, age, variable from tb1; # 多显示一列,该列的值为变量,变量的定义在前面实现 从后面的[]扩展:
where
select name, age from tb1 where id in (,,) # id = 或id=,或id= 集合(,,)
select name, age from tb1 where id <> # <>不等于(小于<或者大于>)
select name, age from tb1 where id not in (,,)
select name, age from tb1 where id between and ; # id in [,] #闭区间集合[,]
select name, age from tb1 where id in (select id from tb2); #
通配符%(多个任意字符), 下划线_(一个任意字符)
select name, age from tb1 where name like "a%" # 以a开头的名字 note: 不能允许这种 where id = and id=, 这样查到的肯定是Null
分页limit [start], count ; start
是从0开始,默认值=;count表示查看的记录数
select name, age from tb1 limit ; #查看第一条数据到第10条数据;
select name, age from tb1 limit ,; #查看第11条数据到第20条数据 简单分页: page = int(input('').strip())
start = (page-)*
select name, age from tb1 limit start, limit offset=; 等效 order by 排序
order by 列名 [asc|desc] # 列名都是整数
select name, age from tb1 order by id desc;# 按id从大到小排序 select name, age from tb1 order by id desc limit ,; # 多列排序
order by cow_1 desc, cow_2 desc # 注意不是order by cow_1, order by cow_2
先按cow_1降序排,如果cow_1有相同的记录,对cow_1相同的记录按cow_2进行降序排 分组group by + 聚合函数 group by part_id: 可以理解成,将part_id相同的折成一组,然后对改组进行聚合
    (等效于将part_id相同的记录折成一条记录,但是该记录的列有要求:只能是part_id,和聚合函数,)
对于相同的记录,可以根据函数count(),max(),min(),sum(),avg()折成一条记录
这些函数称为聚合函数 example1:
用户表,部门表,查看每个部门有多少人
select part_id, count(id), max(id) from tb1 group by part_id; 分组group by + 聚合函数 + 二次刷选(having)
对聚合函数结果进行二次刷选时,必须用having
select part_id, count(id) from tb1 group by part_id having count(id) >
# 刷选出部门人数大于1的部门 连表操作
将所有表select出来,然后告诉他们的关系
select * from user, department where user.part_id = department.id 推荐:left join ...on (之前版本性能更好,目前版本两者性能是一致)
#将所有表select 出来; select * from tb1 left join tb2
# on tb1.part_id = tb2.id 告诉他们表之间的关系 select * from user left join department on user.part_id = department.id 连表操作left join 和right join, inner join 区别
select * from tb1 left join tb2 where ...
tb1会全部显示,左边的表会全部显示; inner join 等效于将实现left join连表,后将Null出现的整行隐藏(不显示) 多张表连接
select * from tb1
left join tb2 where ... # 此时只能告诉tb1和tb2的关系
left join tb3 where ... # 此时可以告诉tb1和tb3的关系,tb2和tb3的关系(此时,已经将tb1,tb2,tb3关联进来了) 连表的时候,如果遇到列名相同,*可变成表名.
select tb1.id,
tb2.id,
tb3.id
from tb1
left join tb2 where ..
left join tb3 where .. 举例: http://images2015.cnblogs.com/blog/425762/201608/425762-20160803224643778-2071849037.png
http://www.cnblogs.com/wupeiqi/articles/5729934.html
学生,班级,老师,课程,成绩表: 学生表:foreign key 班级表
班级表:
课程表:foreign key 老师
成绩表:学生id, 课程id, 分数; foreign key 学生表, foreign key 课程表 # 临时表
将查询得到的数据,作为一个临时表;在内存中存储,但并没有写到硬盘上。
语法:
(select * from tb1 where age > ) as B;
select sid from(select * from tb1 where age > ) as B; # 增加显示列(前提:select score from tb1 where name="yuwen"只能是一个值) select id, (select score from tb1 where name="yuwen") from tb2;
tb1和tb2不是同一表,增加的显示列不是处理同一个表同一行的数据 # 增加显示列
select id, (select score from tb1 as s2 where s2.id= s1.id) as score from
tb1 as s1 # 最外层的循环,一行一行数据
# 里面的循环,每一行中的一列一列 # 条件语句
case when min(num) < then else end as c # mysql的三元运算
if(True, ,)
# 计算课程平均分从高到底显示,显示任课老师
avg(if(isnull(score.num), , score.num))
# 因为为空的时候无法计算 select score.course_id, course.cname, teacher.tname, avg(if(isnull(score.num), , score.num)) as average from score
left join course on score.course_id=course.cid
left join teacher on course.teacher_id=teacher.tid
group by score.course_id
order by average desc; # 此时可以用average,但是在select里面不能用average # 双重循环
select * from
(select
A.student_id,
A.course_id,
A.num,
(select B.num from score as B where B.course_id = A.course_id order by B.num desc limit ,) as frist_s,
(select B.num from score as B where B.course_id = A.course_id order by B.num desc limit ,) as second_s
from score as A)
as C where C.num >= C.second_s # 多表操作,直接用连表,会比较好理解和设置where # # 查询没有学过李平老师课学生的姓名,id---没有学过李平老师任何一门课的学生
先刷选出选过李平老师任意一门课的学生, # 碰到Not,一定要在最顶一层进行Not, 在学生表里面not in (学过老师课程学生的id,还有进行分组过滤掉重复的)
# 先查到李平老师教过哪些课; -- select student.sid,student.sname from student where student.sid not in (
-- select student_id from score
-- where score.course_id in (select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.tname='李平老师')
-- group by student_id); # where group by having : 先进行where一次刷选,再进行group having刷选 # union 上下连表(union自动去重,union all则不会去重)
select id, name from tb1
union
select sid, sname frou stb1;

[oldboy-django][2深入django]mysql查询语句--原生sql的更多相关文章

  1. Django框架08 /聚合查询、分组、F/Q查询、原生sql相关

    Django框架08 /聚合查询.分组.F/Q查询.原生sql相关 目录 Django框架08 /聚合查询.分组.F/Q查询.原生sql相关 1. 聚合查询 2. 分组 3. F查询和Q查询 4. o ...

  2. MySQL查询语句执行过程及性能优化(JOIN/ORDER BY)-图

    http://blog.csdn.net/iefreer/article/details/12622097 MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY) 标签 ...

  3. mysql查询语句,通过limit来限制查询的行数。

    mysql查询语句,通过limit来限制查询的行数. 例如: select name from usertb where age > 20 limit 0, 1; //限制从第一条开始,显示1条 ...

  4. MYSQL查询语句大全集锦

    MYSQL查询语句大全集锦 1:使用SHOW语句找出在服务器上当前存在什么数据库: mysql> SHOW DATABASES; 2:2.创建一个数据库MYSQLDATA mysql> C ...

  5. MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介

    网站或服务的性能关键点很大程度在于数据库的设计(假设你选择了合适的语言开发框架)以及如何查询数据上. 我们知道MySQL的性能优化方法,一般有建立索引.规避复杂联合查询.设置冗余字段.建立中间表.查询 ...

  6. MySQL查询语句执行过程及性能优化-查询过程及优化方法(JOIN/ORDER BY)

    在上一篇文章MySQL查询语句执行过程及性能优化-基本概念和EXPLAIN语句简介中介绍了EXPLAIN语句,并举了一个慢查询例子:

  7. mysql查询语句集

    1. mysql 查询出某字段的值不为空的语句 1.不为空 select * from table where id <> ""; select * from tabl ...

  8. [转]MySQL查询语句执行过程详解

    Mysql查询语句执行原理 数据库查询语句如何执行?语法分析:首先进行语法分析,对使用sql表示的查询进行语法分析,生成查询语法分析树.语义检查:检查sql中所涉及的对象以及是否在数据库中存在,用户是 ...

  9. Mysql查询语句中字符型字段不区分大小写解决方法

    项目中和前端联调的时候,发现Mysql查询语句中字符型字段值过滤是不区分大小写的,之前没有关注过这个设置,特意去网上看了下,原因是Mysql中“COLLATE”属性区分大小写,而该属性默认值为“utf ...

随机推荐

  1. 微信小程序(底部导航的实现)

    详情请看官方文档介绍: https://mp.weixin.qq.com/debug/wxadoc/dev/framework/config.html 在根目录配置文件app.json中配置底部导航: ...

  2. SpringMVC-响应数据和结果视图

    返回值分类 1. 字符串 controller 方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址. 2. void 在 controller 方法形参上可以定义 request 和 ...

  3. SQL小知识_长期总结

    1. 左联接右联接区别 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner ...

  4. 前端开发APP,从HBuilder开始~

    内容简介 介绍目前前端人员开发app的几种方法,具体介绍hbuilder开发app,一扇赞新的大门~ 无所不能的js 最开始js仅仅局限于网页上一些效果,操作网页内容等, 但是nodejs把js带入了 ...

  5. IATHook

    IATHookClass.h #pragma once #include <Windows.h> class IATHookClass { private: DWORD oldAddr; ...

  6. 题解 P3367 【【模板】并查集】

    #include<iostream> #include<cstdio> using namespace std; int n,m,x,y,z; ]; //f[i]表示i的祖先 ...

  7. restful api 规范

  8. centos下修改docker连接docker_host默认方式为tls方式

    1.安装docker,请参考官网文档 centos下安装docker 2.安装完成应该可以使用docker的各种命令连接docker host.docker host运行在本机上,但与localhos ...

  9. nginx安装与部署

    1:安装工具包 wget.vim和gcc yum install -y wget yum install -y vim-enhanced yum install -y make cmake gcc g ...

  10. Cisco交换机与路由器命令总结

    1.查看信息 show version    查看版本及引导信息 show running-config     查看运行设置 show startup-config     查看开机设置 show ...