SQL语句执行顺序

  1. from
  2. join
  3. on
  4. where
  5. group by(开始使用select中的别名,后面的语句中都可以使用)
  6. avg,sum....
  7. having
  8. select
  9. distinct
  10. order by
  11. limit

distinct

  1. -- col1, col2, col3值相同的数据进行去重, distinct必须放置所有列的最前面
  2. select distinct col1, col2, col3 from table;
  3. -- count去重
  4. select count(distinct col1) from table;

group by

根据多个字段分组: 将指定分组字段值都相等的记录视为一组

  1. select province, city, sum(people) from t group by province, city;

order by

根据多个字段排序: 先根据前前驱字段排序, 前驱字段顺序相同, 再根据后续字段排序

  1. select province, city, people from t order by people, city;

JSON字段

  1. UPDATE tb_project SET column_name = JSON_REPLACE(column_name, '$.javaCode', 'value');

事务

普通事务

  1. -- 开启事务
  2. begin;
  3. start transaction;
  4. set autocommit = 0;
  5. -- 设置回滚点
  6. savepoint sp;
  7. -- 回滚到回滚点
  8. rollback to sp;
  9. -- 回滚所有操作
  10. rollback;
  11. -- 提交
  12. commit;
  13. -- 查看当前事物级别
  14. SELECT @@tx_isolation;
  15. -- 设置read uncommitted级别
  16. set session transaction isolation level read uncommitted;
  17. -- 设置read committed级别
  18. set session transaction isolation level read committed;
  19. -- 设置repeatable read级别
  20. set session transaction isolation level repeatable read;
  21. -- 设置serializable级别
  22. set session transaction isolation level serializable;

XA事务

  1. -- 开启事务, 这个'1'就相当于是事务ID, 将事务置于ACTIVE状态
  2. XA START '1';
  3. -- 或者
  4. XA BEGIN '1';
  5. -- 对一个ACTIVE状态的XA事务, 执行构成事务的SQL语句
  6. -- execute your sql
  7. -- 布一个XA END指令, 将事务置于IDLE状态
  8. XA END '1';
  9. -- 对于IDLE状态的XACT事务, 执行XA PREPARED指令, 将事务置于PREPARED状态
  10. -- PREPARED状态的事务可以用XA RECOVER指令列出, 列出的事务ID会包含formatID, gtrid_length, bqual_lenght, data
  11. XA PREPARE '1';
  12. -- 对于PREPARED状态的XA事务, 可以进行提交或者回滚
  13. XA COMMIT '1';
  14. xa ROLLBACK '1';

创建索引

  1. -- 创建索引
  2. -- index_type: UNIQUE | FULLTEXT | SPATIAL
  3. -- index_method: USING [BTREE | HASH | RTREE]
  4. -- index_column_name: column_name [(length)] [ASC | DESC]
  5. CREATE [index_type] INDEX index_name [index_method] ON table_name (index_column_name, ...);
  6. -- 删除索引
  7. DROP INDEX index_name ON talbe_name;

常用函数

  1. -- 返回参数列表中一个不为null的值(合并)
  2. select coalesce(null, null, 'result')
  3. -- 条件函数(写法1
  4. case column
  5. when '1' then 'one'
  6. when '2' then 'two'
  7. else 'other' end
  8. -- 条件函数(写法2
  9. case when column = '1' then 'one'
  10. when column = '2' then 'two'
  11. else 'other' end
  12. -- 聚合函数
  13. count(*)
  14. avg(column)
  15. sum(column)
  16. group_concat([distinct] column [order by asc/desc column] [separator '分隔符'])
  17. -- 字符串拼接
  18. select concat('1', '2', '3');
  19. -- 字符串截取
  20. select substr('abcd', 2);
  21. -- 进制转换
  22. select conv(15, 10, 16);
  23. -- 求模
  24. select 3 mod 2;

时间类型

类型 所占空间(byte) 取值范围
year 1 1901 到 2155
time 3 -838:59:59 到 838:59:59
date 4 1000-01-01 到 9999-12-31
timestamp 4 1970-01-01 00:00:01.000000 到 2038-01-19 03:14:07.999999
datetime 8 1000-01-01 00:00:00.000000 到 9999-12-31 23:59:59.999999

其他

  1. -- 查询建表语句
  2. SHOW CREATE TABLE table_name;
  3. -- 查看表详细信息
  4. DESC table_name;
  5. -- 查看表索引
  6. SHOW INDEX FROM table_name;
  7. -- 修改表字段
  8. ALTER TABLE table_name MODIFY column_name int NOT NULL DEFAULT '0';