python—day40 子查询

Posted kermitjam

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python—day40 子查询相关的知识,希望对你有一定的参考价值。

子查询:把一个查询语句用括号括起来,当作另外一条查询语句的条件去用,成为子查询;

 1 -- 子查询:把一个查询语句用括号括起来,当做另外一条查询语句的条件去用,称为子查询
 2 -- SELECT emp.name from emp INNER JOIN dep on emp.dep_id = dep.id where dep.name = 技术;
 3 -- select name from emp where dep_id =
 4 -- (select id from dep where name = 技术);
 5 
 6 -- #查询平均年龄在25岁以上的部门名
 7 -- select dep_id from emp group by dep_id having avg(age) > 25;
 8 -- select name from dep where id in
 9 -- (select dep_id from emp group by dep_id having avg(age) > 25);
10 
11 -- select * from emp inner join dep on emp.dep_id = dep.id;
12 -- select dep.name from emp inner join dep on emp.dep_id = dep.id group by dep.name having avg(age)>25;
13 
14 -- select * from emp where exists (
15 --     select id from dep where id > 3
16 -- );
17 
18 -- select dep.name from emp inner join dep on emp.dep_id = dep.id group by dep_id having count(dep_id)<2;

 

1 -- select t2.id,t2.name,t2,age,t1.max_date from emp as t2 inner JOIN 
2 -- -- 把查出来的每个部门最大入职时间当成一个虚拟表
3 -- (select post,max(hire_date) as max_date from emp group by post)as t1;
4 -- on t2.id = t1.id where t2.hire_date = t1.hire_date;
5 
6 select t1.id,t1.name,t1.age,t1.sex,t1.post,t1.hire_date,t2.max_date from emp as t1 INNER JOIN 
7 (select post,max(hire_date) as max_date from emp group by post) as t2
8 ON t1.post = t2.post where 
9 t1.hire_date = t2.max_date;

pymysql模块
可以在python文件中直接使用模块来修改mysql数据;



# import pymysql
#
#
# conn = pymysql.connect(
# host = ‘127.0.0.1‘,
# port = 3306,
# user = ‘root‘,
# password = ‘123‘,
# database = ‘school‘,
# charset = ‘utf8‘
# )


# cursor = conn.cursor(pymysql.cursors.DictCursor)
# # rows = cursor.execute(‘show tables;‘)
# rows = cursor.execute(‘select * from class;‘)
# print(rows)

#打印第一行的语句,接着一行一行打印
# print(cursor.fetchone())
# print(cursor.fetchone())

#从头到尾或者接着上面打印的后面内容全部打印
# print(cursor.fetchall())

#可以打印指定的条数
# print(cursor.fetchmany(3))

# print(cursor.fetchall())
# absolute 绝对定位
# cursor.scroll(2,‘absolute‘)
# print(cursor.fetchone())

# print(cursor.fetchall())

#relative 相对定位
# print(cursor.fetchone())
# cursor.scroll(1,‘relative‘)
# print(cursor.fetchone())
#
#
# cursor.close()
#
# conn.close()

 


















































以上是关于python—day40 子查询的主要内容,如果未能解决你的问题,请参考以下文章

python全栈开发,Day40(进程间通信(队列和管道),进程间的数据共享Manager,进程池Pool)

你如何在 python 中处理 graphql 查询和片段?

Oracle Day04 子查询

Day06 子查询

Wordpress查询从文章/页面中提取片段

Python代码阅读(第40篇):通过两个列表生成字典