Python
Python开发
Python语言
python — 表的操作(二)
1.单表查询
单表查询语法:
select distinct 字段1,字段2... from 表名
where 条件
group by field
having 筛选
order by field
limit 限制条数
关键字执行的优先级:
from
where
group by
select
distinct
having
order by
limit
1.1 简单查询
1.select语句:
select * from 表;
select 字段,字段.. from 表;
select distinct 字段,字段.. from 表; # 按照查出来的字段去重
select 字段*5 from 表; # 给查出来的字段做四则运算 (字段时int类型)
select 字段 as 新名字,字段 as 新名字 from 表; # 给查出来的字段重命名
select 字段 新名字 from 表;给查出来的字段重命名
2.定义显示的格式 concat
1.concat() 函数 用于字符串拼接
例:select concat('姓名: ',emp_name,' 年薪: ', salary*12) as annual_salary
from employee;
2.concat_ws() 第一个参数为分隔符
例:select concat_ws(':',emp_name,salary*12) as annual_salary
from employee;
1.2 where语句
where语句不能与聚合函数合用
1.比较运算
> 、< 、= 、>= 、<= 、!= 或<> (不等于)
用于数值比较(不会用于做字符串的比较)
2.范围筛选
1.多选一 :字段名 in (值1,值2,值3)
in (值1,值2,值3) 相当于 or
例:select * from employee where salary in (20000,30000,3000,19000,18000,17000)
2.在一个模糊的范围里
1.在一个数值区间 between 值1 and 值2
# 查薪资在1w-2w之间的所有人的名字
select emp_name from employee where salary between 10000 and 20000;
2.字符串的模糊查询 like
通配符 % 匹配任意长度的任意内容
select * from employee where emp_name like '程%'