数据操作

插入数据(记录): 用insert;

补充:插入查询结果: insert into 表名(字段1,字段2,...字段n) select (字段1,字段2,...字段n) where ...;

更新数据update

语法: update 表名 set 字段1=值1,字段2=值2 where condition;

删除数据delete:delete from 表名 where condition;

查询数据select:

单表查询:

语法:

  1. select distinct 字段1,字段2... from 表名
  2.  
  3.             where 条件
  4.  
  5.             group by field
  6.  
  7.             having 筛选
  8.  
  9.             order by field
  10.  
  11.             limit 限制条数;

关键字的执行优先级:

  1. from
  2. where
  3. group by
  4. having
  5. select
  6. distinct
  7. order by
  8. limit
  9.  
  10. # 1.找到表:from
    # 2.通过where指定的约束条件,去文件/表中取出一条条记录
    # 3.将取出的一条条记录进程分组 group by,如果没有group by,则整体作为一组
    # 4.将分组的结果进行having过滤
    # 5.执行 select
    # 6.去重
    # 7.将结果按顺序排序:order by
    # 8.限制结果的显示条数

简单查询:

  1. #创建表
  2. create table employee(
  3. id int not null unique auto_increment,
  4. name varchar(20) not null,
  5. sex enum('male','female') not null default 'male', # 大部分是男的
  6. age int(3) unsigned not null default 28,
  7. hire_date date not null,
  8. post varchar(50),
  9. post_comment varchar(100),
  10. salary double(15,2),
  11. office int,
  12. depart_id int
  13. );
  1. #插入记录
  2. #三个部门:教学,销售,运营
  3. insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values
  4. ('egon','male',18,'','老男孩驻沙河办事处外交大使',7300.33,401,1),
  5. ('alex','male',78,'','teacher',1000000.31,401,1),
  6. ('wupeiqi','male',81,'','teacher',8300,401,1),
  7. ('yuanhao','male',73,'','teacher',3500,401,1),
  8. ('liwenzhou','male',28,'','teacher',2100,401,1),
  9. ('jingliyang','female',18,'','teacher',9000,401,1),
  10. ('jinxin','male',18,'','teacher',30000,401,1),
  11. ('成龙','male',48,'','teacher',10000,401,1),
  12.  
  13. ('歪歪','female',48,'','sale',3000.13,402,2),
  14. ('丫丫','female',38,'','sale',2000.35,402,2),
  15. ('丁丁','female',18,'','sale',1000.37,402,2),
  16. ('星星','female',18,'','sale',3000.29,402,2),
  17. ('格格','female',28,'','sale',4000.33,402,2),
  18.  
  19. ('张野','male',28,'','operation',10000.13,403,3),
  20. ('程咬金','male',18,'','operation',20000,403,3),
  21. ('程咬银','female',18,'','operation',19000,403,3),
  22. ('程咬铜','male',18,'','operation',18000,403,3),
  23. ('程咬铁','female',18,'','operation',17000,403,3)
  24. ;

查询操作:

  1. # 避免重复 distinct
  2. select distinct post from employee;
  3.  
  4. # 通过四则运算查询
  5. select name,salary*12 from employee;
  6. select name,salary*12 as Annual_salary from employee;
  7. select name,salary*12 Annual_salary from employee; # as Annual_salary是给 salary*12 起了一个别名;as 可省略
  8.  
  9. # 定义显示格式 (只是改变了显示格式,不会改变数据在数据库的保存格式)
  10. concat() 函数用于链接字符串
  11. select concat("员工号:",id,",","姓名:",name) as info,concat("年薪:",salary*12) as annual_salary from employee;
  12. concat_ws() # 第一个参数可以作为分隔符
  13. select concat_ws(":",name,salary*12) as annual_salary from employee;

where约束:

  1. where语句中可以使用:
  2. 1. 比较运算符:>、<、>=、<=、 !=、( <>也表示不等于)
  3. 2. between 10 and 20 # 值在10到20之间
  4. 3. in(80,90,100) # 值是80或90或100
  5. 4. like "neo%"
  6. pattern可以是%或_
  7. %表示任意个任意字符
  8. _表示一个任意字符
  9. 5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and, or, not

主要用法:

where约束:

  1. # 单条件查询:
  2. select name from employee where post="sale";
  3.  
  4. # 多条件查询:
  5. select name,salary from employee where post="teacher" and salary>10000;
  6.  
  7. # 关键字between and
  8. select name,salary from employee where salary between 10000 and 20000;
  9. select name,salary from employee where salary not between 10000 and 20000;
  10.  
  11. # 关键字 is Null:(判断某个字段是否为NULL不能用等号,要用is)
  12. select name,post_comment from employee where post_comment is Null;
  13. select name,post_comment from employee where post_comment is not Null;
    # MySQL中,空字符串不等于 NULL,NULL是单独的数据类型;判断Null的时候必须用 is,如: where id is Null; 
  14.  
  15. # 关键字in集合查询:
  16. select name,salary from employee where salary in (3000,4000,9000);
  17. select name,salary from employee where salary not in (3000,4000,9000);
  18.  
  19. # 关键字like模糊查询:
  20. 通配符:%
  21. select * from employee where name like "eg%";
  22. 通配符:_
  23. select * from employee where name like "ale_";

分组查询:group by

  1. # 分组发生在where之后,即分组是基于where之后得到的记录而进行的
  2. # 分组指的是将所有记录按照某个相同字段进行归类,比如针对员工信息表的职位分组,或者按照性别进行分组等

ONLY_FULL_GROUP_BY

  1. # 查看MySQL 5.7默认的sql_mode如下:
  2. mysql> select @@global.sql_mode;
  3. #ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  4.  
  5. # 如果不设置ONLY_FULL_GROUP_BY,select的查询结果默认值是组内的第一条记录,这样显然是没有意义的;
  6.  
  7. # 设置 ONLY_FULL_GROUP_BY模式:
  8. set global sql_mode="ONLY_FULL_GROUP_BY";
  9.  
  10. # 注意: ONLY_FULL_GROUP_BY 的语义就是确定 select target list中的多有的值都是明确语义,简单来说,在ONLY_FULL_GROUP_BY模式下,target list中的值要么来自聚合函数的结果,要么来自 group by list中的表达式的值(group_concat)
  11.  
  12. # 去掉ONLY_FULL_GROUP_BY模式的设置方法:
  13. mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

聚合函数

  1. # 聚合函数聚合的是组的内容;如果没有进行 group by分组,则默认所以记录是一组,所以此时也能用聚合函数
  2. max()
  3. min()
  4. avg()
  5. sum()
  6. count()

示例:

  1. select post,count(id) from employee group by post; # 只能查看分组依据的字段和使用聚合函数
  2. # 注意:我们按照post字段分组,那么select查询的字段只能是post,想要获取组内的其他相关信息,需要借助函数
  3.  
  4. # group by关键字和 group_concat() 函数一起使用
  5. select post,group_concat(name) as emp_members from employee group by post; # 按照岗位分组,并查看组内成员名
  6.  
  7. # group by和聚合函数一起使用
  8. select post,avg(salary) as average_salary from employee group by post; # 按照岗位分组,并查看每个组的平均工资
  9.  
  10. # 没有分组的聚合函数:
  11. select count(*) from employee;
  12. select avg(salary) from employee;

另外:如果我们用unique的字段作为分组的依据,则每条记录自成一组,这种分组也就没了意义;多条记录之间的某个字段值相同,该字段通常用来作为分组的依据

having过滤:

  1. # having和where不一样的地方:
  2. # 1. 执行优先级:where>group by >having
  3. # 2. where 发生在分组 group by 之前,因而where中可以有任意字段,但是绝对不能使用聚合函数
  4. # 3. having发生在分组group by之后,因而having中可以使用分组的字段,但却无法直接取到其他字段,其他字段需要使用聚合函数

having中也可以用where中的逻辑,例如 and,or 等;having 跟where 用法一样,只不过having是分组之后的过滤

错误用法示例:

  1. mysql> select * from employee having salary > 100000;
  2. ERROR 1463 (42000): Non-grouping field 'salary' is used in HAVING clause # 报错; having前面必须要有 group by
  3.  
  4. mysql> select post,group_concat(name) from employee group by post having salary > 10000; #错误,分组后无法直接取到salary字段
  5. ERROR 1054 (42S22): Unknown column 'salary' in 'having clause'

正确用法如下:

  1. # 1. 查询各岗位平均薪资大于10000的岗位名、平均工资
  2. select post,avg(salary) as average_salary from employee group by post having avg(salary) > 10000;
  3.  
  4. # 2. 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资
  5. select post,avg(salary) as average_salary from employee group by post having avg(salary) between 10000 and 20000;
  6.  
  7. # having的用法就是英语里面的定语从句

order by排序:

  1. select * from employee order by 字段 asc; #升序排;默认
  2. select * from employee order by 字段 desc; #降序排
  3.  
  4. order by 字段1 asc,字段2 desc; # 先按照字段1升序排,如果字段1的值相同则按照字段2降序排
  5. e.g. select * from employee order by age asc,id desc;

执行顺序证明:

  1. select distinct post,count(id) as emp_number from db1.employee
  2. where salary>1000
  3. group by post
  4. having count(id)>2 # having中的count(id)不能用 emp_number 来代替,因为是先执行 having后执行 distinct,所以此时还没有 emp_number这个东西
  5. order by emp_number desc # order by 中的count(id) 可以用 emp_number来代替,因为是先执行distinct后执行的order,执行完distinct之后就已经有了 emp_number
  6. ;
  7.  
  8. # 所以,优先级顺序是: from > where > group by > having > distinct > order by

limit限制条数:不管是书写顺序还是执行顺序,limit都是在最后

  1. select * from employee limit 3; # 3是限制条数;默认初始位置为0
  2. select * from employee limit 0,3; # 从0开始打印3个 (不包含0)
  3.  
  4. # 工资最高的那三个人的信息:
  5. select * from employee order by salary desc limit 3;
  6.  
  7. # 分页打印:
  8. select * from employee limit 0,5;
  9. select * from employee limit 5,5;
  10. select * from employee limit 10,5;
  11. select * from employee limit 15,5;

正则查询regexp: (regexp应该是regular expressioin的缩写吧)

  1. # select * from employee where name regexp "^jin.*(g|n)$"; # jin开头,并且 g或者n结尾

多表查询:(本质就是连表,通过连表将多张有关系的表连接在一起,得到一张虚拟表)

先建两个表,用于下面所有的操作测试

  1. # 建表
  2. create table department(
  3. id int,
  4. name varchar(20)
  5. );
  6.  
  7. create table employee(
  8. id int primary key auto_increment,
  9. name varchar(20),
  10. sex enum('male','female') not null default 'male',
  11. age int,
  12. dep_id int
  13. );
  14.  
  15. #插入数据
  16. insert into department values
  17. (200,'技术'),
  18. (201,'人力资源'),
  19. (202,'销售'),
  20. (203,'运营');
  21.  
  22. insert into employee(name,sex,age,dep_id) values
  23. ('egon','male',18,200),
  24. ('alex','female',48,201),
  25. ('wupeiqi','male',38,201),
  26. ('yuanhao','female',28,202),
  27. ('liwenzhou','male',18,200),
  28. ('jingliyang','female',18,204)
  29. ;

连接方式:

  1. # 1. 内连接: 只取两张表的共同部分
  2. select * from employee inner join department on employee.dep_id = department.id; # 表employee内连接到表department,按照表employee中dep_id字段等于表department中id字段的方式连接
  3.  
  4. # 2. 左链接:在内链接的基础上保留左表的记录
  5. select * from employee left join department on employee.dep_id = department.id;
  6.  
  7. # 3. 右链接:在内链接的基础上保留右表的记录
  8. select * from employee right join department on employee.dep_id = department.id;
  9.  
  10. # 4. 全外链接: 在内连接的基础上左右两表的记录都保存
  11. select * from employee left join department on employee.dep_id = department.id union select * from employee right join department on employee.dep_id = department.id;

内连接:

左连接:

右连接:

全外连接:

多表查询示例:

笛卡尔积:

  1. select * from employee,department;

多表查询原理:

  1. select * from employee inner join department on employee.dep_id = department.id;
  2.  
  3. # 通过这种方式能得到一个整合了表employee和表department的虚拟表

  1. # 再对上面得到的虚拟表进行操作
  2. select department.name,avg(age) from employee inner join department on employee.dep_id = department.id group by department.name having avg(age) > 30;
  3.  
  4. # 多表查询:把有关系的表通过连接的方式拼成一个整体(虚拟表),进而进行相应的关联查询(因为此时已经是一长表了)

SQL逻辑查询语句执行顺序:

一、SELECT语句关键字的定义顺序:

  1. select distinct <select_list>
  2. from <left_table>
  3. <join type> join <right_table>
  4. on <join_condition>
  5. where <where_condition>
  6. group by <group_by_list>
  7. having <having_condition>
  8. order by <order_by_condition>
  9. limit <limit_number>;

二、SELECT语句关键字的执行顺序:

  1. 第一步: from <left_table>
  2. 第二步: on <join_condition>
  3. 第三步: <join_type> join <right_table>
  4. 第四步: where <where_condition>
  5. 第五步: group by <group_by_list>
  6. 第六步: having <having_condition>
  7. 第七步: select
  8. 第八步: distinct <select_list>
  9. 第九步: order by <order_by_condition>
  10. 第十步: limit <limit_number>

具体可参考: http://www.cnblogs.com/linhaifeng/articles/7372774.html

子查询:

1. 带 in 关键字的查询:

  1. # 查询平均年龄在25岁以上的部门名
  2. select name from department where id in
  3. (select dep_id from employee group by dep_id having avg(age) > 25);
  4. # (select dep_id from employee group by dep_id having avg(age) > 25)会有一个返回值,符合过滤条件的 dep_id;where id in (select dep_id from employee group by dep_id having avg(age) > 25) 就类似于 where id in (1,2,3)
  5.  
  6. # 查看技术部员工姓名
  7. select name from employee where dep_id =
  8. (select id from department where name="技术");
  9.  
  10. # 查看不足一人的部门名
  11. # 分析:不足1人就是没有人
  12. select name from department where id not in
  13. (select distinct dep_id from employee) ;
  14. # (select distinct dep_id from employee) 通过去重得到有人的部门id, where id not in ...取反,即 department的id没有在有人的部门id里面

2. 带比较运算符的子查询

  1. # 查询大于所有人平均年龄的员工名和年龄
  2. select name,age from employee where age >
  3. (select avg(age) from employee);
  4. # where后面不能直接写成 where age > avg(age),因为where里面不能使用聚合函数;所以先通过 (select avg(age) from employee)拿到 avg(age)

3. 带exists关键字的子查询 (exists是用于判断是否存在的,返回的类似于bool值)

  1. select * from employee where exists
  2. (select id from department where name="技术"); # 如果(select id from department where name="技术")成立(存在,此时where exists语句返回True),就执行 select * from employee; 如果不存在,就不执行select * 语句
    # exists也可以not 取反

select 查询语句可以用括号括起来,再用 as 起一个别名,就能当作一张表(临时表)来使用,如下:

  1. select * from (select name,age,sex from employee) as t1;

以另外一张employee表为例说明:

  1. # 查询每个部门最新入职的那名员工
  2.  
  3. 报错:
  4. select * from employee as t1
  5. inner join
  6. (select post,max(hire_date) from employee group by post) as t2
  7. on t1.post=t2.post
  8. where t1.hire_date=t2.max(hire_date); # 报错原因:where中不能有聚合函数

  9. 正确:
  10. select * from employee as t1
  11. inner join
  12. (select post,max(hire_date) as new_hire from employee group by post) as t2
  13. on t1.post = t2.post
  14. where t1.hire_date = t2.new_hire; # 取别名后就是单纯的调用了

权限管理:略

Navicat工具:

批量加注释:ctrl+?键

批量去注释:ctrl+shift+?键

pymysql模块

pymysql基本使用:

通过pymysql模块能够在python程序中操作MySQL数据库;pymysql模块本质就是一个套接字客户端软件

  1. import pymysql
  2.  
  3. username = input("username>>>:").strip()
  4. password = input("password>>>:").strip()
  5.  
  6. # 建链接
  7. conn = pymysql.connect(host="192.168.18.2",port=3306,user="root",password="",db="db4",charset="utf8") # 得到一个链接对象; # charset中的utf8不能加 - ,因为mysql中没加
  8.  
  9. # 拿到一个游标(cursor)
  10. cursor = conn.cursor() # 得到一个游标对象
  11.  
  12. # 给游标提交命令,执行sql语句
  13. sql = "select * from userinfo where username='%s' and password='%s' " %(username,password) # sql语句中的username和password要和db4.userinfo这张表中的字段一样
    print(sql)
  14. rows = cursor.execute(sql) # 把sql语句提交给cursor去执行; # execute() 不是执行的结果,而是受影响的行数(rows)
  15.  
  16. cursor.close()
  17. conn.close() # 把资源回收
  18.  
  19. # 进行判断
  20. if rows:
  21. print("登录成功")
  22. else:
  23. print("登录失败")

但上面的程序有一个漏洞:

  1. # 在MySQL中, --空格 后面的内容都会被注释掉(两个横杠后面跟一个空格),所以在你的python程序中输入:
  2. username>>>:neo' -- xxxx
  3. 不输密码,也能够成功登录

并且

  1. # 输入:
  2. username>>>:xxx' or 1=1 -- hahahaah
  3. 不输密码,也可以登录

解决办法:利用pymysql模块的sql注入

pymysql模块之sql注入:

  1. import pymysql
  2.  
  3. username = input("username>>>:").strip()
  4. password = input("password>>>:").strip()
  5.  
  6. # 建链接
  7. conn = pymysql.connect(host="192.168.18.2",port=3306,user="root",password="",db="db4",charset="utf8")
  8.  
  9. # 拿到一个游标(cursor)
  10. cursor = conn.cursor() # 得到一个游标对象
  11.  
  12. # 给游标提交命令,执行sql语句
  13. sql = select * from userinfo where username=%s and password=%s # 不要自己拼接字符串,利用 pymysql的execute拼接字符串; # 占位符也不要再加引号
  14. rows = cursor.execute(sql,(username,password)) # 第一个参数还是传入要执行的sql语句;第二个参数传入一个元组,元组里面放入sql语句里面的占位符,通过这种方式拼接字符串,能把其中的特殊字符处理掉
  15.  
  16. cursor.close()
  17. conn.close()
  18.  
  19. # 进行判断
  20. if rows:
  21. print("登录成功")
  22. else:
  23. print("登录失败")

pymysql模块之增删改:

  1. import pymysql
  2.  
  3. # 建链接
  4. conn = pymysql.connect(host="192.168.18.2",port=3306,user="root",password="",db="db4",charset="utf8")
  5.  
  6. # 拿到游标
  7. cursor = conn.cursor()
  8.  
  9. # 执行sql语句
  10. # 增删改
  11. sql = "insert userinfo(username,password) values(%s,%s)"
  12. print(sql)
  13. rows = cursor.execute(sql,("abc",""))
  14.  
  15. conn.commit() # 修改的数据要生效,必须在cursor.conn关闭之前 conn.commit()
  16.  
  17. # 关闭
  18. cursor.close()
  19. conn.close()

插入多条记录:

  1. # 插入多条记录
  2. rows = cursor.executemany(sql,[("egon1",""),("egon2",""),("egon3","")]) # 利用executemany(),列表中放入多个元组

lastrowid用法:查询你即将插入的数据是从第几行开始的

  1. import pymysql
  2.  
  3. # 建链接
  4. conn = pymysql.connect(host="192.168.18.2",port=3306,user="root",password="",db="db4",charset="utf8")
  5.  
  6. # 拿到游标
  7. cursor = conn.cursor()
  8.  
  9. # 执行sql语句
  10. sql = "insert userinfo(username,password) values(%s,%s)"
  11.  
  12. # 插入多条记录
  13. rows = cursor.executemany(sql,[("egon7",""),("egon8",""),("egon9","")]) # 利用executemany(),列表中放入多个元组
  14. print(cursor.lastrowid) # cursor.lastrowid 是你上面代码插入的时候,是从第几行开始插入的
  15.  
  16. conn.commit() # 修改的数据要生效,必须在cursor,conn关闭之前 conn.commit()
  17.  
  18. # 关闭
  19. cursor.close()
  20. conn.close()

删改就是把上述例子中的sql语句改成删改的sql语句就行了

pymysql模块之查询

  1. import pymysql
  2.  
  3. conn = pymysql.connect(host="192.168.18.2",port=3306,user="root",password="",db="db4",charset="utf8")
  4.  
  5. cursor = conn.cursor(pymysql.cursors.DictCursor) # cursor()中如果什么都不写,查询出来的数据是元组的形式;如果指明了 pymysql.cursors.DictCursor,查询结果是字典的形式,字典的key是表的字段
  6.  
  7. rows = cursor.execute("select * from userinfo")
  8. print(cursor.fetchone())
  9. print(cursor.fetchone())
  10. print(cursor.fetchone())
  11. print(cursor.fetchone())
  12. print(cursor.fetchone())
  13. print(cursor.fetchone())
  14. print(cursor.fetchone())
  15. print(cursor.fetchone())
  16. # 运行过程分析: cursor.execute("select * from userinfo")给MySQL服务端发送了查询语句,服务端查完之后把查询结果返回给服务端,服务端收到后把全部结果放到了管道里面,fetchone()一次就取出一条结果;取完之后再去就是None
  17.  
  18. # fetch还有两种用法:
  19. # 1. cursor.fetchmany(3) # 一次取3条;取出来的结果放到一个列表中,由于已经指定了 pymysql.cursors.DictCursor,所以列表中是一个个字典
  20. # 2. cursor.fetchall() # 一次全部取完,结果放到一个列表中;取完之后再fetchall会得到一个空列表
  21.  
  22. cursor.close()
  23. conn.close()

fetchone:

fetchmany:

fetchall:

cursor.scroll用法:移动管道中的光标

  1. import pymysql
  2.  
  3. conn = pymysql.connect(host="192.168.18.2",port=3306,user="root",password="",db="db4",charset="utf8")
  4.  
  5. cursor = conn.cursor(pymysql.cursors.DictCursor)
  6.  
  7. rows = cursor.execute("select * from userinfo")
  8.  
  9. # cursor.scroll(3,mode="absolute") # 相对绝对位置移动:从管道最开始的位置跳过去3条
  10. # cursor.scroll(3,mode="relative") # 相对当前位置移动:从光标所在管道的当前位置跳过去3条
  11.  
  12. cursor.scroll(3,mode="absolute")
  13. print(cursor.fetchone()) # 跳过前三条,直接从第四条开始取
  14.  
  15. cursor.close()
  16. conn.close()

相对绝对位置移动

相对当前位置移动

  1. print(cursor.fetchone())
  2. cursor.scroll(3,mode="relative")
  3. print(cursor.fetchone()) # 从第二条开始跳过取3个开始取

MySQL内置功能:

视图:

视图一个虚拟表(非真实存在),其本质是【根据SQL语句获取动态的数据集,并为其命名】,用户使用时只需使用【名称】即可获取结果集,可以将结果当作表来使用;但是不推荐使用视图,因为扩展SQL极不方便

创建视图:

  1. # 语法: create view 视图名称 as sql语句
  2. create view teacher_view as select tid from teacher where tname='李平老师';
  3.  
  4. #于是查询李平老师教授的课程名的sql可以改写为
  5. mysql> select cname from course where teacher_id = (select tid from teacher_view);

修改视图(往视图中插入数据),原始表也跟着改

修改视图:

  1. 语法:ALTER VIEW 视图名称 AS SQL语句
  2. mysql> alter view teacher_view as select * from course where cid>3;

删除视图:

  1. # 语法:DROP VIEW 视图名称
  2.  
  3. DROP VIEW teacher_view

函数:

date_format(date相关字段,date格式)  # 第一个参数写date的相关字段,第二个参数写所需要的date格式,如:"%Y-%m-%d";

datediff(current_date,sale_date)   #  current_date和sale_date之间的天数间隔

示例:

  1. select item_id,count(distinct date_format(sale_date,"%Y-%m-%d")) as day_num from txn
  2. where datediff(current_date,sale_date) <=10
  3. group by item_id
  4. having day_num >=5
  5. order by day_num desc;
  6.  
  7. # current_date也是一个函数,表示当天的日期

控制流函数:

1、case when condition1 then result1 ... else default end

# 如果 conditionN是真,则返回 resultN,否则返回default

2、case test when value1 then result1... else default end

# 如果test 和valueN相等,则返回 resultN,否则返回default

如下:

查询班级信息,包括班级id、班级名称、年级、年级级别(1为低年级,2为中年级,3为高年级)

  1. select cid as 班级id,caption as 班级名称,gname as 年级,
  2. (case grade_id when 1 then "低" when 2 then "中" else "高" end) as "年级级别" from class inner join
  3. class_grade on class.grade_id=class_grade.gid;

MySQL:记录的增删改查、单表查询、约束条件、多表查询、连表、子查询、pymysql模块、MySQL内置功能的更多相关文章

  1. mysql 记录的增删改查

    MySQL数据操作: DML ======================================================== 在MySQL管理软件中,可以通过SQL语句中的DML语言 ...

  2. MySQL—记录的增删改查操作

    1.插入记录: 方法一:INSERT [INTO] tbl_name [(col_name,···)] {VALUES|VALUE} ({expr |DEFAULT},···),(···),··· 例 ...

  3. Django项目的创建与介绍.应用的创建与介绍.启动项目.pycharm创建启动项目.生命周期.三件套.静态文件.请求及数据.配置Mysql完成数据迁移.单表ORM记录的增删改查

    一.Django项目的创建与介绍 ''' 安装Django #在cmd中输入pip3 #出现这个错误Fatal error in launcher: Unable to create process ...

  4. mysql对库,表及记录的增删改查

    破解密码 #1.关闭mysqlnet stop mysqlmysql还在运行时需要输入命令关闭,也可以手动去服务关闭 #2.重新启动mysqld --skip-grant-tables跳过权限 #3m ...

  5. java jdbc 连接mysql数据库 实现增删改查

    好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...

  6. shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)

    shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查) Shell脚本与MySQL数据库交互(增删改查) # 环境准备:安装mariadb 数据库 [ro ...

  7. 手撸Mysql原生语句--增删改查

    mysql数据库的增删改查有以下的几种的情况, 1.DDL语句 数据库定义语言: 数据库.表.视图.索引.存储过程,例如CREATE DROP ALTER SHOW 2.DML语句 数据库操纵语言: ...

  8. (转)MySql中监视增删改查和查看日志记录

    转载地址为:http://blog.51cto.com/hades02/1641652 首先在命令行输入 show global variables like '%general%' ,然后出现下面的 ...

  9. mysql简单使用增删改查

    修改配置文件 在my.in配置文件 找到client 指的是mysql客户端 port3306 default -charachter-set=utf-8 default -charachter-se ...

随机推荐

  1. 转 PHP编程过程中需要了解的this,self,parent的区别

    {一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object Oriented Programming)现已经成为编程人员的一项基本技能.利用OOP的思想进行 ...

  2. Codeforces Round #235 (Div. 2) D (dp)

    以为是组合,后来看着像数位dp,又不知道怎么让它不重复用..然后就没思路 了. 其实状压就可以了 状压是可以确定一个数的使用顺序的 利用01也可以确定所有的数的使用以及不重复 dp[i+1<&l ...

  3. $.extend(x,y); 函数用法介绍。

    第一篇资料:  转自: https://www.cnblogs.com/yuqingfamily/p/5813650.html 语法:jQuery.extend( [deep ], target, o ...

  4. 自学 iOS - 三十天三十个 Swift 项目 第三天

    做了这个小demo 之后  感觉OC 和swift 还是有很大的差别的 自己还是要去多看些swift的语法 用的不是很熟练 1.这个demo 的资源文件 我都是用原工程的 2.同样的自定义cell 的 ...

  5. 移动设备访问使用百度js跳转

    以下为代码,可放置在网站foot底部文件,或者haead顶部文件,建议将代码放在网站顶部,这样可以实现手机访问立即跳转! <script src="http://siteapp.bai ...

  6. Node.js搭建静态服务器

    let http = require('http'); let url = require('url'); let fs = require('fs'); let path = require('pa ...

  7. 在Windows7下编译调试C#程序

    要在 命令行下编译C#代码,要配置一下 1.在环境变量下新建一个变量 参数名: csc 参数值:C:\Windows\Microsoft.NET\Framework\v4.0.30319 2.在系统变 ...

  8. InChatter系统之服务端的Windows服务寄宿方式(三)

    为了部署的方便,我们开发Windows服务的服务寄宿程序,这样我们的服务便可以作为系统服务,随着系统的启动和关闭而启动和关闭,而避免了其他的设置,同时在服务的终止时(如系统关闭等)能及时处理服务的关闭 ...

  9. centOS linux 下PHP编译安装详解

    一.下载PHP源码包 wget http://php.net/distributions/php-5.6.3.tar.gz   二.添加依赖应用 yum install -y gcc gcc-c++ ...

  10. ALTER SCHEMA - 修改一个模式的定义

    SYNOPSIS ALTER SCHEMA name RENAME TO newname DESCRIPTION 描述 ALTER SCHEMA 修改一个模式的定义. 现在它唯一的功能就是重命名模式. ...