二、 子查询

(一个问题一个问题解决)

把一个查询语句用括号括起来,当做另外一条查询语句的条件去用,称为子查询

select name from emp where dep_id = (select id from dep where name="技术");  #子查询

select emp.name from emp inner join dep on emp.dep_id = dep.id where dep.name="技术";  #链表

#查询平均年龄在25岁以上的部门名

select name from dep where id in (select dep_id from emp group by dep_id having avg(age) > 25);   #子查询

select dep.name from emp inner join dep on emp.dep_id = dep.id group by dep.name having avg(age) > 25;  #链表

#查看不足2人的部门名(子查询得到的是有人的部门id)

select * from emp where exists (select id from dep where id > 3); #exists用法,当()返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询(empty set)

#查询每个部门最新入职的那位员工

select t1.id,t1.name,t1.post,t1.hire_date,t2.post,t2.max_date from emp as t1 inner join (select post,max(hire_date) as max_date from emp group by post) as t2 on t1.post = t2.post where t1.hire_date = t2.max_date;

三、 IDE工具(navicat)介绍

1、 ER图表(Entity-Relationship)

2、 模型;导出sql

3、 查询; 格式美化sql

四、 pymysql模块(安装与查询)

1、安装pymysql(python专用的mysql客户端套接字)

pip3 install pymysql

2、mysql 查询

import pymysql

#连接

client=pymysql.connect(

host='127.0.0.1',

port=3306,

user='root',

password='jxtz0927',

database='db40',

charset='utf8'  )  # 防止乱码

# 游标

# cursor=client.cursor()

#执行.完毕返回的结果集默认以元组显示

cursor=client.cursor(pymysql.cursors.DictCursor)

#执行.完毕返回的结果集默认以字典形式显示

#执行sql语句

# rows=cursor.execute('show tables;')  #rows 为受影响的行数

rows=cursor.execute('select * from class;')

# 执行sql语句,打印受影响的行数

print(rows)

print(cursor.fetchone())   #取一条

print(cursor.fetchmany(2))  #取几条

print(cursor.fetchall())   #取全部

cursor.scroll(3,'absolute')  #mode='absolute',绝对模式,从最开始位置向后移三条

cursor.scroll(1,'relative')  #mode='relative',相对模式,从当前位置向后移一条

cursor.close()

client.close()

五、 pymysql模块(防止sql注入问题)

1、错误做法, 自行对字符串进行拼接,引发sql注入问题 (name= egon' --asdfg; name=xxxx'or 1=1--asdfg)

import pymysql

conn=pymysql.connect(

host='127.0.0.1',

port=3306,

user='root',

password='123',

database='db42',

charset='utf8'

)

cursor=conn.cursor(pymysql.cursors.DictCursor)

inp_user=input('用户名>>:').strip() #inp_user=""

inp_pwd=input('密码>>:').strip() #inp_pwd=""

sql="select * from user where username='%s' and password='%s'" %(inp_user,inp_pwd) #注意这里的%s必须加引号。自行拼接,引发sql注入问题.

print(sql)

rows=cursor.execute(sql)

if rows:

print('登录成功')

else:

print('登录失败')

cursor.close()

conn.close()

2、在服务端防止sql注入问题:不要自己拼接字符串,让pymysql模块去拼接

import pymysql

conn=pymysql.connect(

host='127.0.0.1',

port=3306,

user='root',

password='123',

database='db42',

charset='utf8'

)

cursor=conn.cursor(pymysql.cursors.DictCursor)

inp_user=input('用户名>>:').strip() #inp_user=""

inp_pwd=input('密码>>:').strip() #inp_pwd=""

sql="select * from user where username=%s and password=%s"

print(sql)

rows=cursor.execute(sql,(inp_user,inp_pwd))

if rows:

print('登录成功')

else:

print('登录失败')

cursor.close()

conn.close()

六、 pymysql模块(增删改)

import pymysql

conn=pymysql.connect(

host='127.0.0.1',

port=3306,

user='root',

password='123',

database='db42',

charset='utf8'

)

cursor=conn.cursor(pymysql.cursors.DictCursor)

sql='update user set username="alexSB" where id=2'

rows=cursor.execute(sql) #改数据

print(rows)

print(cursor.lastrowid)

sql='insert into user(username,password) values(%s,%s)'

rows=cursor.executemany(sql,[('lwz','123'),('evia','455'),('lsd','333')])  #一次插入多行记录

print(rows)

print(cursor.lastrowid)  #显示插到哪行了(id)last row id(即最新行的id)

conn.commit()   # 只有commit提交才会完成真正的修改

cursor.close()

conn.close()

子查询 navicat可视化,pymysql用法相关推荐

  1. PHP MySQL 子查询(subquery)语法与用法实例

    MySQL 子查询 子查询是将一个 SELECT 语句的查询结果作为中间结果,供另一个 SQL 语句调用.MySQL 支持 SQL 标准要求的所有子查询格式和操作,也扩展了特有的几种特性. 子查询没有 ...

  2. MySQL学习-子查询及limit分页

    子查询及limit分页 操作的表 1.where后面嵌套子查询 2.from后面嵌套子查询 3.select后面嵌套子查询 4.union的用法 5.limit以及通用分页SQL 操作的表 mysql ...

  3. python 全栈开发,Day63(子查询,MySQl创建用户和授权,可视化工具Navicat的使用,pymysql模块的使用)...

    昨日内容回顾 外键的变种三种关系:多对一:左表的多 对右表一 成立左边的一 对右表多 不成立foreign key(从表的id) refreences 主表的(id)多对多建立第三张表(foreign ...

  4. ORACLE子查询的多种用法

    一.用于查询 第一个是最普通的子查询,子查询 (内查询) 在主查询之前一次执行完成.子查询的结果被主查询(外查询)使用.子查询一般是返回单行,要是返回多行,就要用到使用多行比较操作符(in,all,a ...

  5. MySQL——子查询用法

    文章目录 MySQL--子查询用法 1.子查询概述 2.子查询的使用 2.1.单行子查询 2.2.多行子查询 2.3.相关子查询 子查询练习题 MySQL--子查询用法 1.子查询概述 子查询简介 子 ...

  6. oracle模糊查询中的regexp_like嵌套子查询用法

    oracle模糊查询中的regexp_like嵌套子查询用法 regexp_like一般用于模糊查询某一列时包含多个查询条件 需求1:在用户表中查询出账号包含650000和230000的用户. sel ...

  7. 关于Oracle Insert 语句的子查询 和 with check option的用法

    今日睇ocp教程  发现 insert语句还可以子查询例如: INSERT INTO (SELECT employee_id, last_name, email, hire_date, job_id, ...

  8. SQL SERVER 子查询的用法

    子查询是一个嵌套在 SELECT.INSERT.UPDATE 或 DELETE 语句或其他子查询中的查询.任何允许使用表达式的地方都可以使用子查询.在此示例中,子查询用作 SELECT 语句中名为 M ...

  9. Mysql基本用法-left join、right join、 inner join、子查询和join-02

    left join #左连接又叫外连接 left join 返回左表中所有记录和右表中连接字段相等的记录  test_user表 phpcvs表 SQL: select * from test_use ...

最新文章

  1. Java HashMap的put操作(Java1.8)
  2. 操作系统:内存连续分配方式采用的几种算法及各自优劣
  3. 【小白的CFD之旅】02 江小白
  4. python文件操作实验报告_20193120 实验三《Python程序设计》实验报告
  5. 在python中可以使用if作为变量名_变量,注释,缩进,细数Python优雅风 | Python基础连载(二)...
  6. 小程序跳转样式布局错乱_小程序页面布局样式元素总结
  7. 嵌入式linux内核开启键盘,- 基于嵌入式Linux内核的特殊矩阵键盘设计完整驱动控制模块方案...
  8. 20.1 shell脚本介绍 20.2 shell脚本结构和执行 20.3 date命令用法 20.4 shell脚本中的变量...
  9. java肯尼亚_对肯尼亚这个国家,你有什么好奇的?对它的第一印象是什么?
  10. 爆炸的符卡洋洋洒洒(01背包)
  11. 使用Faker.NET仿造数据
  12. 国际结算习题集及答案
  13. 8 个顶级网络攻击地图以及如何使用它们
  14. SpringCloud项目启动
  15. BOM 定时器+回调函数
  16. electron 自定义右键菜单
  17. 2N个数排成一行(每个数有2个), 2个1之间有1个数,2个2 之间有2个数,...2个N之间有N个数... 例312132
  18. Product Requirement Document
  19. CSP-S 蒟蒻啊qaq
  20. 机器学习项目之数据清洗

热门文章

  1. 穿越之我是码农 1024 篇
  2. 2008年超级计算机排名,2008年Opteron超级计算机性能将突破1petaflop
  3. 一行代码教你撩妹手到擒来html+css+js烟花告白3D相册(含音乐+可自定义文字)520表白/七夕情人节/求婚...
  4. uniapp 聊天记录插入的两种方式
  5. Hutool-Excel大数据生成-XXOO
  6. 【解惑】专科生在IT的发展之路
  7. docker+nginx搭建私有云笔记leanote
  8. android 三种定位方式
  9. 关于组长/leader的一些反省和自我批判
  10. GHOST使用教程图解