一、子查询

        #1:子查询是将一个查询语句嵌套在另一个查询语句中。
#2:内层查询语句的查询结果,可以为外层查询语句提供查询条件。
#3:子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
#4:还可以包含比较运算符:= 、 !=、> 、<等
1 带IN关键字的子查询 #查询平均年龄在25岁以上的部门名
select id,name from department
where id in
(select dep_id from employee group by dep_id having avg(age) > 25); #查看技术部员工姓名
select name from employee
where dep_id in
(select id from department where name='技术'); #查看不足1人的部门名(子查询得到的是有人的部门id)
select name from department where id not in (select distinct dep_id from employee); 2 带比较运算符的子查询 #比较运算符:=、!=、>、>=、<、<=、<>
#查询大于所有人平均年龄的员工名与年龄
mysql> select name,age from emp where age > (select avg(age) from emp);
+---------+------+
| name | age |
+---------+------+
| alex | 48 |
| wupeiqi | 38 |
+---------+------+
2 rows in set (0.00 sec) #查询大于部门内平均年龄的员工名、年龄
select t1.name,t1.age from emp t1
inner join
(select dep_id,avg(age) avg_age from emp group by dep_id) t2
on t1.dep_id = t2.dep_id
where t1.age > t2.avg_age; 3 带EXISTS关键字的子查询 EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。
而是返回一个真假值。True或False
当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询 #department表中存在dept_id=203,Ture
mysql> select * from employee
-> where exists
-> (select id from department where id=200);
+----+------------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+------------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | female | 18 | 204 |
+----+------------+--------+------+--------+ #department表中存在dept_id=205,False
mysql> select * from employee
-> where exists
-> (select id from department where id=204);
Empty set (0.00 sec) 二、pymysql模块 1.链接、执行sql、关闭(游标)
import pymysql
user=input('用户名: ').strip()
pwd=input('密码: ').strip() conn=pymysql.connect(host='localhost',user='root',password='123',database='egon',charset='utf8')
cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示
#cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #执行sql语句
sql='select * from userinfo where name="%s" and password="%s"' %(user,pwd) #注意%s需要加引号
print(sql)
res=cursor.execute(sql) #执行sql语句,返回sql查询成功的记录数目
print(res) cursor.close()
conn.close() if res:
print('登录成功')
else:
print('登录失败') 2.execute()之sql注入 注意:符号--会注释掉它之后的sql,正确的语法:--后至少有一个任意字符
根本原理:就根据程序的字符串拼接name='%s',我们输入一个xxx' -- haha,用我们输入的xxx加'在程序中拼接成一个判断条件name='xxx' -- haha'
最后那一个空格,在一条sql语句中如果遇到select * from t1 where id > 3 -- and name='egon';则--之后的条件被注释掉了 #1、sql注入之:用户存在,绕过密码
egon' -- 任意字符 #2、sql注入之:用户不存在,绕过用户与密码
xxx' or 1=1 -- 任意字符 解决方法: # 原来是我们对sql进行字符串拼接
# sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd)
# print(sql)
# res=cursor.execute(sql) #改写为(execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了)
sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引号,因为pymysql会自动为我们加上
res=cursor.execute(sql,[user,pwd]) #pymysql模块自动帮我们解决sql注入的问题,只要我们按照pymysql的规矩来。 3.增、删、改:conn.commit() import pymysql
conn=pymysql.connect(host='localhost',user='root',password='123',database='egon')
cursor=conn.cursor() #执行sql语句
#part1
# sql='insert into userinfo(name,password) values("root","123456");'
# res=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数
# print(res) #part2
# sql='insert into userinfo(name,password) values(%s,%s);'
# res=cursor.execute(sql,("root","123456")) #执行sql语句,返回sql影响成功的行数
# print(res) #part3
sql='insert into userinfo(name,password) values(%s,%s);'
res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) #执行sql语句,返回sql影响成功的行数
print(res) conn.commit() #提交后才发现表中插入记录成功
cursor.close()
conn.close() 4.获取插入的最后一条数据的自增ID import pymysql
conn=pymysql.connect(host='localhost',user='root',password='123',database='egon')
cursor=conn.cursor() sql='insert into userinfo(name,password) values("xxx","123");'
rows=cursor.execute(sql)
print(cursor.lastrowid) #在插入语句后查看 conn.commit() cursor.close()
conn.close()

多表查询与pymysql的更多相关文章

  1. Mysql基础(五):多表查询、pymysql模块

    目录 数据库04 /多表查询.pymysql模块 1. 笛卡尔积 2. 连表查询 3. 子查询 4. pymysql模块 数据库04 /多表查询.pymysql模块 1. 笛卡尔积 将两表所有的数据一 ...

  2. 数据库04 /多表查询、pymysql模块

    数据库04 /多表查询.pymysql模块 目录 数据库04 /多表查询.pymysql模块 1. 笛卡尔积 2. 连表查询 2.1 inner join 内连接 2.2 left join 左连接 ...

  3. day43 多表查询和pymysql

    复习 增删改查全语法 # 增 insert into db1.t1(字段2, 字段1, ..., 字段n)|省略 values (值2, 值1, ..., 值n)|(值1, 值2, ..., 值n)[ ...

  4. mysql之多表查询和pymysql模块

    一 多表查找方法 1 交叉连接:不使用任何的判断条件,生成笛卡尔积.第一个表的行数乘以第二个表的行数就等于笛卡尔积结果集的行数. mysql> select * from student,cla ...

  5. MySQL多表查询,pymysql模块。

    一 多表查询: 首先什么是多表查询: 我们在实际工作中,不可能把数据都存入一个表中,那么又需要这些表之间有一定的关联,因为表与表之间的数据是相关联的,所以就要用到我们的外键将多表连接到一起,那么我们更 ...

  6. Mysql数据多表查询及pymysql的使用

    Exists关键字表示存在,在使用exists关键字时,内增查询语句不返回查询记录,而是返回一个真假值,True或者False,返回True外层语句才会进行查询:返回False时,外层查询语句不会进行 ...

  7. 多表查询、可视化工具、pymysql模块

    create table dep( id int primary key auto_increment, name varchar(16), work varchar(16) ); create ta ...

  8. 数据库 --- 4 多表查询 ,Navicat工具 , pymysql模块

    一.多表查询 1.笛卡儿积 查询 2.连接 语法: ①inner    显示可构成连接的数据 mysql> select employee.id,employee.name,department ...

  9. MySQL:记录的增删改查、单表查询、约束条件、多表查询、连表、子查询、pymysql模块、MySQL内置功能

    数据操作 插入数据(记录): 用insert: 补充:插入查询结果: insert into 表名(字段1,字段2,...字段n) select (字段1,字段2,...字段n) where ...; ...

随机推荐

  1. 「题目代码」P1029~P1033(Java)

    1029 C基础-求解方程 import java.util.*; import java.io.*; import java.math.BigInteger; public class Main { ...

  2. C#3DES加密了解一下

    最近一个项目中,因为服务端是用的java开发的,客户端是用的C#,由于通信部分采用到了3DES加密,所以做个记录,以备以后需要的时候直接用. 这是对方(java)的加密算法,和网上流传的代码也差不多( ...

  3. Centos6 使用yum快速搭建LAMP环境

      1.安装Apache [root@localhost ~]# yum -y install httpd # 开机自启动 [root@localhost ~]# chkconfig httpd on ...

  4. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  5. float与定位的区别

    float和绝对定位的区别 CSS中脱离文档流,也就是将元素从普通的布局排版中拿走,其他盒子在定位的时候,会当做脱离文档流的元素不存在而进行定位.1 需要注意的是,使用float脱离文档流时,其他盒子 ...

  6. P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \ ...

  7. BZOJ day2_plus

    大半夜的刷b站,好爽啊... 突破十九题 1008105110591088117911911192143218761951196821402242243824562463276128184720

  8. 特殊密码锁 的通过码是:(请注意,在openjudge上提交了程序并且通过以后,就可以下载到通过码。请注意看公告里关于编程作业的说明)

    // // main.cpp // openjudge特殊密码锁 // // Created by suway on 17/11/20. // Copyright © 2017年 suway. // ...

  9. 用静态工厂的方法实例化bean

    //代码如下: package com.timo.domain; public class ClientService { //use static factory method create bea ...

  10. sls文件

    http://www.ituring.com.cn/article/42238 只是数据而已 深入学习之前,明白SLS文件只是结构化的数据而已是很有用的.看懂和编写SLS文件不需要理解这一点,但会让你 ...