day43

多表查询

笛卡尔积——不经常用

将两表所有的数据一一对应,生成一张大表

select * from dep,emp; # 两个表拼一起
select * from dep,emp where dep.id = emp.dep_id; # 找到两表之间对应的关系记录
select * from dep,emp where dep.id = emp.dep_id and dep.name = '技术'; # 筛选部门名称为技术的记录
select emp.name from dep,emp where dep.id = emp.dep_id and dep.name = '技术'; # 拿到筛选后的记录的员工姓名字段数据

连表查询

inner join 内连接
第一步:连表
select * from dep inner join emp on dep.id = emp.dep_id 第二步:过滤
select * from dep inner join emp on dep.id = emp.dep_id where dep.name = '技术'; 第三步:找对应字段数据
select emp.name from dep inner join emp on dep.id=emp.dep_id where dep.name = '技术';
left join 左连接

left join左边的表为主表,主表记录必须全部显示,辅表没办法对应上的就通过null来补全

select * from dep left join emp on dep.id=emp.dep_id;
right join 右连接

right join右边的表为主表,主表记录必须全部显示,辅表没办法对应上的就通过null来补全

select * from dep right join emp on dep.id=emp.dep_id;
union 全连接
select * from dep left emp on dep.id=emp.dep_id union select * from dep right join emp on dep.id=emp.dep_id

子查询

一个查询结果集作为另一个查询的条件

select name from emp where dep_id = (select id from dep where name = '技术');
  • 子查询是将一个查询语句嵌套在另一个查询语句中。
  • 内层查询语句的查询结果,可以为外层查询语句提供查询条件
  • 子查询中可以包含:in、not in、any、all、exists、not exists等关键字
  • 还可以包含比较运算符:=、!=、>、<
带in的查询
select id,name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25);
带any查询
自己尝试:select title from s2 where s2.id = any(select s2_id from s3 where s1_id = (select s1.id from s1 where name = '太白'));

百度:
mysql> CREATE TABLE t(a INT, b INT);
Query OK, 0 rows affected (0.68 sec) mysql> INSERT t VALUES (1, 1),(1, 2),(1, 3),(1, 4);
Query OK, 4 rows affected (0.13 sec)
Records: 4 Duplicates: 0 Warnings: 0 mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| myset |
| s1 |
| s2 |
| s3 |
| shirt |
| stu |
| t |
| t1 |
| t2 |
+---------------+
9 rows in set (0.00 sec) mysql> select * from t;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
+------+------+
4 rows in set (0.00 sec) mysql> select b from t where b >= 2 and b<= 3;
+------+
| b |
+------+
| 2 |
| 3 |
+------+
2 rows in set (0.00 sec) mysql> select * from t where b >= any (select b from t where b >= 2 and b<= 3); # b>=2 or b>=3
+------+------+
| a | b |
+------+------+
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |
+------+------+
3 rows in set (0.00 sec) mysql> select * from t where b <= any (select b from t where b >= 2 and b<= 3); # b<=2 or b<=3
+------+------+
| a | b |
+------+------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
+------+------+
带some的查询

与any的效果相同

带all的查询
与上面any实例相同
SELECT * FROM t WHERE
b >= ALL (SELECT b FROM t WHERE b >= 2 AND b <= 3); # b>=2 and b>=3
结果:
1 3
1 4
SELECT * FROM t WHERE
b <= ALL (SELECT b FROM t WHERE b >= 2 AND b <= 3); # b<=2 and b<=3
结果:
1 1
1 2
带比较运算符的查询
select name,age from emp where age > (select avg(age) from emp);
带exists关键字的查询

exists 关键字意思是存在,在使用exists关键字时,内层查询语句不返回查询的记录,而是返回一个真假值。True或False

当返回True时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询。还可以写not exists 和exists的效果是反的

mysql> select * from employee where exists (select id from department where id=200);

Navicat工具自己使用

#1. 测试+链接数据库
#2. 新建库
#3. 新建表,新增字段+类型+约束
#4. 设计表:外键
#5. 新建查询
#6. 备份库/表

公司一般用的创建数据库模型的软件

PowerDesigner是一个做开发设计很常用的工具软件,同时还有Rose也可以,都是当前软件开发最著名的建模设计及软件之一。

pymysql模块

pymysql python连接mysql的客户端

总结:
import pymysql
conn = pymysql.connect(
host='127.0.0.1', # 主机
prot=3306, # 端口号
user='root', # 用户名
password='666', # 密码
database='day43', # 需要连接的库
charset='utf8'
)
cursor = conn.cursor() sql = 'select * from dep;'
ret = cursor.execute(sql) # ret是受影响的行数
print(cursor.fetchall()) # 取出所有的
print(cursor.fetchmany(3)) # 取出3条(多条)
print(cursor.fetchone()) # 取出单条 cursor.scroll(3,'absolute') # 绝对移动,按照数据最开始位置往下移动3条
cursor.scroll(3,'relative') # 相对移动,按照当前光标位置往下移动3条 conn.commit() # 增删改操作时,需要进行提交
cursor.close() #关闭游标
conn.close() #关闭连接 sql注入:解决方案
cursor.execute(sql,[参数1,参数2.。。])
查询元组显示
import pymysql

conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='222',
database='day43',
charset='utf8'
)
cursor = conn.cursor()
# 默认游标取出的数据是((),)
# DictCursor 对应的数据结构[{},],如果用的是fetcheone
,那么结果是{}
sql = 'select * from dep;' ret = cursor.execute(sql) print(ret) # ret 是受影响的行数
# print(cursor.fetchall()) # 打印出所有的数据
print(cursor.fetchmany(4)) # 打印出3条数据,默认一条
# print(cursor.fetchone()) # 打印出一条 # 打印有光标,会随着打印的数据移动
# cursor.scroll(4, 'absolute') # 绝对移动,相对于数据最开始的位置进行光标的移动,不可等于或大于表最的大行数,不然报错
# cursor.scroll(2, 'relative') # 相对移动,按照光标当前位置来进行光标的移动,不可等于或大于表的最大行数,不然报错 # print(cursor.fetchone()) # 打印出一条,如果没有数据会返回None
# print(cursor.fetchmany(4)) # 打印出多条数据,默认一条,里面的个数多余实际个数不会报错,只会全部打印出来,没有返回一个空元组或者空列表
# print(cursor.fetchall()) # 打印出所有的数据,打印多余的数据,没用数据返回一个空元组或者空列表
cursor.close() #关闭游标
conn.close() #关闭连接
查询字典显示
import pymysql

conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='222',
database='day43',
charset='utf8'
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
# 默认游标取出的数据是((),)
# DictCursor 对应的数据结构[{},],如果用的是fetcheone,那么结果是{}
sql = 'select * from dep;' ret = cursor.execute(sql) print(ret) # ret 是受影响的行数
# print(cursor.fetchall()) # 打印出所有的数据
print(cursor.fetchmany(4)) # 打印出3条数据,默认一条
# print(cursor.fetchone()) # 打印出一条 # 打印有光标,会随着打印的数据移动
# cursor.scroll(4, 'absolute') # 绝对移动,相对于数据最开始的位置进行光标的移动,不可等于或大于表最的大行数,不然报错
# cursor.scroll(2, 'relative') # 相对移动,按照光标当前位置来进行光标的移动,不可等于或大于表最的大行数,不然报错 # print(cursor.fetchone()) # 打印出一条,如果没有数据会返回None
# print(cursor.fetchmany(4)) # 打印出多条数据,默认一条,里面的个数多余实际个数不会报错,只会全部打印出来,没有返回一个空元组或者空列表
# print(cursor.fetchall()) # 打印出所有的数据,打印多余的数据,没用数据返回一个空元组或者空列表
cursor.close() #关闭游标
conn.close() #关闭连接
增删改查操作
import pymysql

conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='222',
database='day43',
charset='utf8'
)
cursor = conn.cursor()
sql = 'insert into t1 values(3,"xx3",18);' ret = cursor.execute(sql) print(ret) # 增删改都必须进行提交操作(commit)
conn.commit()
cursor.close() #关闭游标
conn.close() #关闭连接
sql注入
import pymysql

conn = pymysql.connect(
host='127.0.0.1',
port=3306,
user='root',
password='222',
database='day43',
charset='utf8'
) while 1:
cursor = conn.cursor(pymysql.cursors.DictCursor)
username = input('请输入用户名:')
password = input('请输入密码:') # sql注入问题
# sql = f"select * from t1 where name='{username}' and age='{password}';" # xx3 '-- :xx3(必须是表里的数据)、'(和前面的'号对应使 (-- )在''号外面注释后面的语句)
# sql = f"select * from t1 where name='xx3 '-- ' and age='{password}';" # asdasd' or 1=1 -- :(asdasd)可以不是表里的数据、(or 1=1 -- )or 1=1使mysql以为查到了数据然后再进行注释后面的语句
# sql = f"select * from t1 where name='asdasd' or 1=1 -- ' and age='{password}';" # 解决方法:
sql = "select * from t1 where name=%s and age=%s;"
ret = cursor.execute(sql, [username, password])
if ret:
print('登录成功!')
else:
print('账号或者密码错误,请重新输入!')
cursor.close() #关闭游标
conn.close() #关闭连接

day43——多表查询、Navicat工具的使用、pymysql模块的更多相关文章

  1. day43 多表查询和pymysql

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

  2. day03 mysql外键 表的三种关系 单表查询 navicat

    day03 mysql navicat   一.完整性约束之     外键 foreign key     一个表(关联表: 是从表)设置了外键字段的值, 对应的是另一个表的一条记录(被关联表: 是主 ...

  3. MySQL多表查询,Navicat使用,pymysql模块,sql注入问题

    一.多表查询 #建表 create table dep( id int, name varchar(20) ); create table emp( id int primary key auto_i ...

  4. Navicat的安装和pymysql模块的使用

    内容回顾 select distinct 字段1,字段2,... from 表名 where 分组之前的过滤条件 group by 分组条件 having 分组之后过滤条件 order by 排序字段 ...

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

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

  6. Navicat工具、pymysql模块 sql注入

    cls超 Navicat工具.pymysql模块 阅读目录 一 IDE工具介绍 二 pymysql模块 一 IDE工具介绍(Navicat) 生产环境还是推荐使用mysql命令行,但为了方便我们测试, ...

  7. 不使用left-join等多表关联查询,只用单表查询和Java程序,简便实现“多表查询”效果

    上次我们提到,不使用left-loin关联查询,可能是为了提高效率或者配置缓存,也可以简化一下sql语句的编写.只写单表查询,sql真得太简单了.问题是,查询多个表的数据还是非常需要的. 因此,存在这 ...

  8. Navicat,SQL注入,pymysql模块

    # 关键字exists(了解) 只返回布尔值 True False 返回True的时候外层查询语句执行 返回False的时候外层查询语句不再执行 select * from emp where exi ...

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

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

随机推荐

  1. learning java Charset 查看支持的字符集类型

    import java.nio.charset.Charset; import java.util.SortedMap; public class CharsetTest { public stati ...

  2. Visual C++ 里的异常处理

    微软Visual C++是Win32最广泛使用的编译器,因此Win32反向器对其内部工作非常熟悉.能够识别编译器生成的粘合代码有助于快速集中于程序员编写的实际代码.它还有助于恢复程序的高级结构.我将集 ...

  3. zabbix显示 get value from agent failed:cannot connetct to xxxx:10050:[4] interrupted system call

    在阿里云上部署的两台云主机,从server上 agent.ping不通agent10050端口,在agent上使用firewalld-cmd 添加了10050端口还不行,关闭了防火墙和selinux也 ...

  4. 【后缀数组】【LuoguP2852】 [USACO06DEC]牛奶模式Milk Patterns

    题目链接 题目描述 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". J ...

  5. linux命令之------Less命令

    Less命令 1)作用:less与more类似,但使用less可以随意浏览文件,而more仅能向前移动,却不能向后移动,而且less在查看之前不会加载整个文件. 2)ctrl+F 向前移动一屏: 3) ...

  6. CSS3之碰撞反弹动画无限运动

    示例代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  7. ajax post data 获取不到数据,注意 content-type的设置 、post/get(转)

    ajax post  data  获取不到数据,注意 content-type的设置 .post/get 关于 jQuery data 传递数据.网上各种获取不到数据,乱码之类的. 好吧今天我也遇到了 ...

  8. 模板 - 部分C++库

    __builtin系列 据说是GCC自带的系列,在本地装有 GNU GCC Compiler 的 Codeblocks 和 Codeforces 等平台都可以使用这些.但是没办法从 Codeblock ...

  9. Linux下的nexus数据迁移

    刚到公司没多久,目前公司有两个项目公用一个nexus的maven私服,现在想把两个私服的jar包拆分开: 我在原私服的nexus服务器中, 1.备份原nexus使用命令 完成tar包的压缩 打包完毕后 ...

  10. 思科 DHCP服务器配置及DHCP中继

    思路: 1.配置 DHCP 客户端 确保每个 PC 为 自动获取IP地址的方式: 2.配置 SW1 # 创建 VLAN 10 , 20 # 将相关的端口,放入到对应的 VLAN : # 配置交换机之间 ...