1、主键
主键值唯一,不可修改,不能为空,删除不能重用
2、数据类型(常用)
char int float date timestamp
3、select
select * from data;
select xx,xxx from data;//取部分行
select * from data limit 100; //限制数量
select * from data order by xx;//升序
select * from data order by xx desc;// 降序
select * from data where xx = xxx;// 条件查询
select * from data where xx = xxx and xxs=xxxs;// 条件查询
select * from data where xx = xxx or xxs=xxxs;// 条件查询
select * from data where xx != xxx;// 不包含
select * from data where xx <> xxx;// 不包含
select * from data where xx > xxx;
select * from data where xx bewteen xxx and xxxx;
select * from data where xx in(A , B);//中文加引号
select * from data where xx not in(A , B);//中文加引号
select * from data where xx like xxx;// 条件模糊查询
select * from data where xx like 'xx%';// 条件模糊查询后面无所谓
4、grounp by,grounp by having 过滤
select * from data group by xx;// 分组,返回一个
select *,count(1) from data group by xx;//计数
select *,count(*) from data group by xx;//计数
select *,count(xxx) from data group by xx;//计数
select *,count(distinct xxx) from data group by xx;//计数去重
select * from data group by xx having count(xxc)>=100;//对结果过滤
select * from data where xxx like '%xx%' group by xx having count(xxx) >50;//复合使用
select *,count(1) from data group by xx having count(if(xxs like '%xxv%',1,null))>50;//结合if使用
5、字符串截取
select left(xx,1),xx from data;//截取第一个字符
select locate('x',xxx),xx from data;//x字符在xxx中第一出现得位置,从1开始
select locate('x',xxx,3),xx from data;//x字符在xxx中第一出现得位置,从3开始
length(xxx) // 字段长度
substr(字符串,开始位置,长度)
6、as别名
“as”关键字用于为数据表和字段指定别名,
语法:
1、“select 字段名 as 别名 from 数据表;”,可为字段指定别名;
2、“select 字段名 from 数据表 as 别名;”,可为表指定别名。
7、case子查询
1.简单函数
CASE [column_name] WHEN [value1] THEN [result1]... ELSE [default] END
2.搜索函数
CASE WHEN [expr] THEN [result1]... ELSE [default] END
搜索函数可以使用表达式 expr 判断,并且返回第一个符合条件的值。
select
case
when xxx1 then xx1
when xxx2 then xx2
when xxx3 then xx3
else xx4
end,
xx from xxx...
8、跨表操作, join
1、直接写
select * from data.表一
where xx = {
select xx from data.表二
where xxx= '条件'
}
2、join使用
select * from data.表一 as x1
join data.表二 as x2 on 条件
select * from data.表一 as x1
left join data.表二 as x2 on 条件
select * from data.表一 as x1
inner join data.表二 as x2 on 条件
9、加载
load data local infile 'csv所在位置' into table data.表
10、时间操作
1、格式转换date_format()
2、加减 date_add(),date.sub()
3、差值 datediff()
select xxTime ,date(xxTime),date_format(xxTime,'%Y-%m-%d'),date_add(xxTime,interval 1 day) from data.表名