码迷,mamicode.com
首页 > 编程语言 > 详细

python—day40 子查询

时间:2018-05-11 20:34:13      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:any   语句   max   style   sel   tables   connect   数据   select   

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

 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 子查询

标签:any   语句   max   style   sel   tables   connect   数据   select   

原文地址:https://www.cnblogs.com/kermitjam/p/9025344.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!