作用:增删查改数据库
select - 选择列
from - 表名
where - 列条件
group by - 组合
having -
distinct -
order by - 顺序倒序
limit - 限制数量
….
union
子查询
from # 确定到底是哪张表where # 根据过来条件 筛选数据select # 拿出筛选出来的数据中的某些字段
select * from User WHRER id=1 ORDER BY id DESC;select 列|from表名|WHRER条件|规则####select列:星号* 表示全部列name,id,points 表示其中的列(无需括号)重复只找一个:DISTINCT name####from表名:User 用户表有时候也会查不到,例如你要查中文的表就要:`用户表` 数字1左边的那个点####wherewhere id >= 3 and id <= 6;select * from emp where id between 3 and 6;# 上2语句完全等价where salary = 20000 or salary = 18000 or salary = 17000;where salary in (20000,18000,17000);# 上2语句完全等价where salary not in (20000,18000,17000); # not 不是where name like '%o%'; # 前后多个任意where name like '_o_'; # 一个任意where name like '____'; # 4个任意where name is Null; # 描述为空where name <> "" ; # 描述不为空# 时间where date > "2022-1-1"where date BETWEEN '2022-01-01' AND '2022-12-31';where date > '2022-01-01'; # <= < > >=to_days(now()) - to_days(create_time) <= 0; # 当天date_sub(curdate(), INTERVAL 7 DAY)<=date(时间字段名); # 7天date_sub(curdate(), INTERVAL 7 DAY)<=date(createtime); # 7天条件多个,要加括号WHERE (startdate < "2022-1-1" or startdate is Null)AND (enddate > "2022-1-1" or enddate is Null)####group by xx 组xx列中同内容分组select * from emp group by post;如果你的MySQL不报错 说明严格模式没有设置show variables like '%mode%';set session 当前窗口有效set global 全局有效set global sql_mode="strict_trans_tables,only_full_group_by";(设置严格模式)####group by xx 组的聚合函数max min avg sum count个数xx列中同内容出现次数总结例子:原表:+----+--------+---------------------+--------+| id | name | date | singin |+----+--------+---------------------+--------+| 1 | 小明 | 2016-04-22 15:25:33 | 1 || 2 | 小王 | 2016-04-20 15:25:47 | 3 || 3 | 小丽 | 2016-04-19 15:26:02 | 2 || 4 | 小王 | 2016-04-07 15:26:14 | 4 || 5 | 小明 | 2016-04-11 15:26:40 | 4 || 6 | 小明 | 2016-04-04 15:26:54 | 2 |+----+--------+---------------------+--------+SELECT name, COUNT(*) FROM table GROUP BY name;输出:+--------+----------+| name | COUNT(*) |+--------+----------+| 小丽 | 1 || 小明 | 3 || 小王 | 2 |+--------+----------+####having和where语法一样having必须在group by后面使用####ORDER BY规则:ORDER BY id DESC 顺序ORDER BY id ASC 倒序select * from emp order by age asc,salary desc; # 先按照age做升序 age相同的情况下再按照salary做升序#### select * from emp G; 当表字段特别多的时候 结果的排版可能会出现混乱的现象 你可以在查询语句加“斜杠G”来规范查询结果####limit 限制展示数据的条数select * from emp limit 5; # 只展示数据的五条,条数select * from emp limit 5,5; #从第六条开始展示五条,起始、条数
results = cursor.fetchone()print (results)
results = cursor.fetchall()for row in results:code = row[0]name = row[1]# 打印结果print("code=%s,name=%s" %(code, name))