MySQL基础随记
增:insert into table_name values(col1,col2,col3);
insert into table_name(col1,col2) values(col1,col2);
删:delete from table_name where ....
改:update table_name set col1='xxx',col2=12 where ....
查: select * from table_name where ....
1. distinct 关键字
在select后面使用,用来修饰查找的列,查找指定的列时,不包括重复的值。
select distinct column_name1,column_name2 from table_name
2. order by关键字
放在sql语句的最后面,用来控制查询结果的排序,可以指定列和排序方式,默认排序方式是ASC(升序),DESC(降序)
select * from table_name order by column_name ASC|DESC
3. 规定返回记录的数目
可用于分页等操作。
//mysql语法,limit关键字放在where之后
select * from table_name where ... limit 10;
4. 模糊查询,like 关键字
放在where之后,作为where的判断条件
select * from where col_name like '%aaa%' //%为通配符,表示任意个任意字符。
SQL通配符:
通配符 | 描述 |
---|---|
% | 代替0个或多个字符 |
_ | 代替一个字符 |
[charlist] | 字符列表中的任何单一字符 |
[^charlist] | 不包含在字符列表中的字符 |
mysql 字符列表的使用
//关键字rlike,not rlike
1. select * from table_name where col_name rlike 'http://[ABC]'; //查找匹配的记录
2. select * from table_name where col_name rlike 'http://[^ABC]'; //查找不匹配的记录
3. select * from table_name where col_name not rlike 'http://[ABC]';
4. select * from table_name where col_name not rlike 'http://[^ABC]';
1 = 4 , 2 = 3 类似于双重否定等于肯定。
5. 范围查找,in、between关键字
这个范围可以是静态指定的范围,也可以是select出来的范围
select * from table_name where col_name in ('xxx','xxxx');//静态范围
select * from table_name where col_name in (select col_name from table_name where ...); //动态范围
between关键字也是指定查找范围:
select * from table_name where age between 10 and 22;
6. 别名
可以给字段取别名,也可以给查询表或临时表取别名。
给字段取别名:
select col_name1 as xxx ,col_name2 as vvv from table_name where ....
//将多列合并取别名
select col1,concat(col2,col3) as newName from table_name where ...
给表取别名:
给查询表取别名一般用于多表查询。
select * from table1 as t1,table2 as t2 where a.id = b.id
7. mysql连接
**内连接(inner join):**获取两个表字段匹配关系的记录,只返回两个表匹配的记录
select t1.col_name1, t2.col_name2 from table1 as t1 inner join table2 as t2 on t1.id=t2.id where ....
先返回on后面条件匹配的记录,然后在执行where匹配的记录,如果不指定col_name使用*的话,则返回两个表的所有字段。
左连接(left join):.***返回左表的全部记录***以及对应右表的记录,如果左表有记录在右表中没有有匹配记录,相应的字段为null
select t1.col_name1, t2.col_name2 from table1 as t1 left join table2 t2 on t1.id=t2.id
右连接与左连接类似。
8. null值比较
- is null
- is not null
- <=> :当两个值相等或都为null时返回true
select * from table_name where col_name is null
9.分组关键字,group by
分组必须与聚合函数一起使用
select col_name1,function(col_name2) from table_name where ... group by col_name3
10.mysql 正则表达式
11.事务
在 MySQL 命令行的默认设置下,事务都是自动提交的,即执行 SQL 语句后就会马上执行 COMMIT 操作。因此要显式地开启一个事务务须使用命令 BEGIN 或 START TRANSACTION,或者执行命令 SET AUTOCOMMIT=0,用来禁止使用当前会话的自动提交。
-
begin :显示的开启一个事务
-
commit :提交事务
-
rollback : 回滚事务,撤销未提交的修改
-
savepoint identifier :允许在事务中创建一个保存点,一个事务中可以有多个 SAVEPOINT
-
release savepoint identifier :删除一个事务的保存点,当没有指定的保存点时,执行该语句会抛出一个异常
-
rollback to identifier 把事务回滚到标记点
-
set transaction用来设置事务的隔离级别。InnoDB 存储引擎提供事务的隔离级别有read uncommited、read comminted、repeatable read 和 serializable
//显示执行事务
begin;
select * from table_name;
...
commit;
12.alter 关键性,修改表或字段
删除字段:
alter table table_name drop 字段名;
添加字段:
alter table table_name add 字段名 数据类型;
alter table table_name add 字段名 数据类型 first; //指定字段位置为第一列
alter table table_name add 字段名 数据类型 after col_name; //指定字段位置在某列之后
修改字段:
alter table table_name modify 字段名 数据类型 not null default 123; //修改指定字段名数据类型
alter table table_name change 旧字段名 新字段名 数据类型; //修改字段名和类型
修改表名:
alter table table_name rename to newName;
13.mysql 聚合函数
函数名 | 功能 |
---|---|
COUNT(col) | 统计查询结果的行数 |
MIN(col) | 查询指定列的最小值 |
MAX(col) | 查询指定列的最大值 |
SUM(col) | 返回指定列的总和 |
AVG(col) | 返回指定列的平均值 |