本篇只记录基础模块,部分模块可以直接调用,部分模块需要复制改写后使用
篇外 SQL
SQL常用语句 增删改查
insert into 表名称 (列名称1, 列名称2) values (值1, 值2)
delete from 表名称 where 列名称 = 值;
update 表名称 SET 列名称 = 新值 WHERE 列名称 = 旧值;
select 列名 from 表名称 where . group by . having . order by . limit .
查询时对内容的处理
cast(id as char) #转换格式输出,
#cast的更多格式:signed, unsigned, decimal, date, time, datetime
right(列1, N) #截取某个字段末N个字符
concat(列1, 列2) #连接多个字段
group_concat(字段名 separator ",") #连接同一字段的多个结果,可指定分隔符
left join去重
基础用法:在A表,不在B表
select a.列1, b.列2 from tabel_a as a
left join table_b as b
on a.列3 = b.列4
where b.列4 is null
高级用法:把【在A表,不在B表】的查询结果为表C,然后表C与其它表left join
(select a.列1, b.列2 from tabel_x as a
left join table_y as b
on a.列3 = b.列4 where b.列4 is null)
select c.列5 from {子句} as c
left join table_z as d
on c.列6 = d.列7 where d.列7 is Null
清空数据表
truncate table table_name
授权
在Navicat的命令列界面输入:
grant drop, alter on table_name to user_name
去除group by限制
MySQL默认限制只允许按select的内容进行group by,可通过查询语句【set @@sql_mode = ‘’】取消限制。该语句可以在查询窗口中输入,也可以在python中修改。
日期相关
curdate() #当前日期
curtime() #当前时间
date(time) #时间转日期
pymysql
连接数据库
连接本机数据库时,ip地址可以填写localhost
部分Python版本只接受关键词参数,不接受位置参数,关键词名称也可能不同
def my_connect():