重看
- 复制数据表(带数据)
mysqldump base --add-drop-table | mysql test_base;
- 创建数据库
create database <name>;
- 使用数据库
use <name>;
- 查看所有数据库
show databases;
计数
- 计算某列中某值有多少行
select count(*) from <> where gender = "男";
- 计算某个列有多少不同值
select count(distinct <column> from <table_name>);
更新
- update
update <table_name> set column=<value> where column=<value>;
删除
- delete
delete from <table_name> where column = <value>;
其他
- 别名
- select column as <name> concat(volumn,"+",volumn_2) as volumn_3 from table_name limt 5;
-
join
-
inner join
-
左表和右表的交集
-
left join
-
返回所有左表数据,左右表未匹配的补null
-
right join
-
union(合并查询结果)
-
select <column_1,column_2> from <table_name> where column_3= union select <column_1,column_2> from <table_name> where column_3=
-
select <> into
-
简单插入
-
select column_1,column_2 into <new_table> from <old_table>
-
多表插入
-
select table_1.column_1,table_2.column_2 into <new_table> from table_1 left join table_2 on table_1.column_3 = table_2.column_4
-
备注: 先into 后 left join
-
创建空表
-
select * into <new_table> from table_1 where 1=0;
-
insert into <> select
-
insert into table_2(name,country) select (name,country) from table_1 where id =1;
-
alter (在已有表中添加、删除或修改列)
-
增加列
-
alter table <table_name> add <column_name> ;
-
删除列
-
alter table <table_name> drop column <column_name>;
-
修改数据类型
-
alter table <table_name> alter column <column_name>
-
创建索引
-
create index <index_name> on <table_name> <column_name>
-
备注 索引名 对应 所有索引 ,即索引名表的属性
-
函数
-
having
-
where无法与aggregate函数一起使用
-
索引过长
-
原因
- mysql 索引长度为 max_length * 3 ,而最大索引长度为767bytes
-
解决方案
-
使用innodb引擎
-
修改索引字段的max_length
-
my.conf
default-storage-engine=INNODBInnodb_large_prefix=on
-
-
-
text作为联合索引的问题
- 由于text是可变长度,所以在做索引时需要指定取作为索引的长度