1.select 查询
2.distinct 去重查询 如 select distinct
3.where 查询条件
4.and or 且 或
5.order by 查询结果排序 (asc desc)
6.insert into 插入数据 如: insert into table_name values ('');
7.update 更新数据 如: update table_name set col = '' [where ];
8.delete 删除数据 如: delete from table_name;
9.top 规定返回查询数目 (sql server用法) 在mysql中使用limit 在oracle中使用where rownum
10.like where中的查询模式 如 like '%' like '_'
11.in 在条件中查询匹配多个值
12.between...and... 选取两个值之间的数据范围
13.join 从两个表或多个表获取查询结果
其中有
inner join(内连接) 当表中存在至少一个匹配时 返回行 (即两个表中满足ON条件的行)
select column_names
from table_name1 inner join table_name2
on table_name1.column = table_name2.column;
left join (左连接) 返回左表中所有的行 即使在右表中没有匹配的行 右表中不存在的为NULL
right join(右连接) 返回右表中所有的行 即使在左表中没有匹配的行 左表中不存在的为NULL
full join(全连接) 只要其中某个表存在匹配 就返回行 为NULL
14.union union all 合并select查询结果 union all不去重 返回所有结果
15.select into
select * into new_table_name
from old_table_name;
16.常见的数据类型
integer int smallint tinyint decimal(size, d) char() varchar() date(yyyymmdd) timestamp
17.SQL约束
not null 不能为空
unique 唯一
primary key 主键约束
foreign key 外键约束
check 限制列中值的范围
default 设置默认值
18.索引 index
create index index_name
on table_name (col_name);
drop index index_name on table_name;
19.truncate 清空表
20.alter table
alter table table_name
add column_name datatype;
alter table table_name
drop column_name;
21.视图
create view view_name as
select column_names
from table_name
where condition;
drop view view_name;
22.date函数
now() 返回当前的日期和时间
curdate() 返回当前日期
curtime() 返回当前时间
date() 提取日期
date_add() 给日期添加指定的时间间隔
date_sub() 给日期减去指定的时间间隔
datediff() 返回两个日期之间的天数
date_format() 用不同的格式显示日期
23.NULL
is null is not null
IFNULL() 若为空 返回第二个参数
COALESCE() 遇到非null值 返回该值 并停止
例:
select COALESCE(null, null, 1, 2) --返回1
select COALESCE(null, null, null, null) --返回null
select if(1=1, 1, 0)
24.mysql中主要的数据类型:文本,数字,日期
text类型:
char() varchar() tinytext() 最大都为255
text, blob,mediumtext, mediumblob, longtext, longblob
enum() 枚举 set 集合
number类型:
tinyint smallint mediumint int bigint float double decimal
date类型:
date() datetime() timestamp() time() year()
25.函数
group by , having
avg() count() max() min() sum() ucase() lcase() mid() len() round()
网友评论