-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
bajins
committed
Aug 2, 2019
1 parent
dffc2d7
commit 7f4db20
Showing
25 changed files
with
383 additions
and
451 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# 存储过程 | ||
# MySQL存储过程 | ||
|
||
## 创建存储过程 | ||
```sql | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# MySQL常用语句 | ||
|
||
## 随机取值 | ||
> 先根据rand()和id获取一遍随机数,再跟原表关联,最后取出值,因为是随机取,所以你的id值越平均,数据量越大,最后limit之前获取的id值就会越接近整表数据量的一半,limit取到的结果也越接近。 | ||
```sql | ||
SELECT * FROM `table1` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `table1`)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id ASC LIMIT 1 | ||
``` | ||
### 最大减去最小值乘以随机值获取随机数 | ||
```sql | ||
SELECT * FROM USER WHERE id >= ( ( SELECT MAX( id ) FROM USER ) - ( SELECT MIN( id ) FROM USER ) ) * RAND( ) + ( SELECT MIN( id ) FROM USER ) LIMIT 1 | ||
``` | ||
> `order by rand()`效率极低,采用JOIN的语法比直接在WHERE中使用函数效率还要高很多 | ||
## 随机插入 | ||
### 连续有限队列随机取 | ||
```sql | ||
update table1 set a = (floor(rand() * 4) + 3) where b = 84; | ||
``` | ||
### 非连续有限队列随机取 | ||
```sql | ||
update table1 set a = elt(floor(rand() * 4) + 1, 5, 7, 11, 13) where b = 84; | ||
``` | ||
|
||
## 查询分组中某字段最大值 | ||
> 因为`group by`后取的一条数据默认是按照主键id排序后的第一条,所以对表先排序,然后再分组 | ||
```sql | ||
SELECT 别名.* FROM (SELECT * FROM 表名 ORDER BY 字段 DESC) AS 别名 GROUP BY 别名.字段; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# MySQL常见问题 | ||
|
||
## GROUP_CONCAT | ||
#### 使用`GROUP_CONCAT`出现: | ||
|
||
> Row 147 was cut by GROUP_CONCAT() | ||
#### 由于MySQL的`GROUP_CONCAT`有默认大小值,先查询一下: | ||
```sql | ||
SHOW VARIABLES LIKE 'group_concat_max_len'; | ||
#或者 | ||
SELECT @@global.group_concat_max_len; | ||
``` | ||
#### 默认值为1024 | ||
| Variable_name | Value | | ||
|-------------|------------| | ||
| group_concat_max_len | 1024 | | ||
|
||
#### 修改默认值大小 | ||
##### 在MySQL配置文件中添加配置: | ||
```bash | ||
#-1为最大值或根据实际需求设置长度 | ||
group_concat_max_len = -1 | ||
``` | ||
|
||
#### 如果是生产环境下,不能擅自重启MySQL服务,则可以通过语句设置`group_concat`的作用范围: | ||
```sql | ||
SET GLOBAL group_concat_max_len=-1; | ||
SET SESSION group_concat_max_len=-1; | ||
``` | ||
## 自定义函数 | ||
### 执行自定义函数报错: | ||
> `This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and | ||
binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)` | ||
|
||
|
||
#### 查看是否开启`log_bin_trust_function_creators` | ||
```sql | ||
SHOW VARIABLES LIKE 'log_bin_trust_function_creators'; | ||
#或者 | ||
SELECT @@global.log_bin_trust_function_creators; | ||
``` | ||
#### 默认关闭 | ||
| Variable_name | Value | | ||
|------------------|-----------------| | ||
|log_bin_trust_function_creators | OFF| | ||
|
||
#### 开启: | ||
##### 在MySQL配置文件中`[mysqld]`节点下加上: | ||
```bash | ||
log_bin_trust_function_creators=1 | ||
``` | ||
#### 如果是生产环境下,不能擅自重启MySQL服务,则可以通过语句开启,重启后无效: | ||
```sql | ||
SET GLOBAL log_bin_trust_function_creators = 1; | ||
#或者 | ||
SET GLOBAL log_bin_trust_function_creators=TRUE; | ||
``` | ||
|
||
|
||
## 表名未忽略大小写 | ||
```bash | ||
vim /etc/my.cnf | ||
``` | ||
### 在`[mysqld]`模块中添加: | ||
```bash | ||
lower_case_table_names = 1 | ||
``` | ||
### 保存并重启mysql | ||
|
||
|
File renamed without changes.
Oops, something went wrong.